MySQL query - select from one, count from another table

Andrew K.

I have database with two tables - 'Warehouses' and 'Boxes'. Each box has field with warehouse code, each Warehouse - 'capacity' field.

The purpose is to find only Warehouses that are "overfilled" (capacity of warehouse is less then number of all boxes with this warehouse code).

So, I count all boxes and join warehouse capacity by this query:

SELECT Warehouses.Code, Warehouses.Capacity, COUNT(Boxes.Code)
FROM `Warehouses` RIGHT JOIN
     `Boxes`
     on Warehouses.Code = Boxes.Warehouse
GROUP BY Boxes.Warehouse

Result:

------------------------------
Code | Capacity | COUNT
------------------------------
1    |    3     | 4
------------------------------
2    |    4     | 2
------------------------------
3    |    7     | 2
------------------------------
4    |    2     | 1
------------------------------

That returns me warehouse's capacity and counts boxes in it, but I don't know how and where to compare these numbers.

Gordon Linoff

You do this in a HAVING clause:

SELECT w.Code, w.Capacity, COUNT(b.Code)
FROM `Warehouses` w LEFT JOIN
     `Boxes` b
     on w.Code = b.Warehouse
GROUP BY w.Code, w.Capacity
HAVING w.Capacity < COUNT(b.Code);

Notes:

  • LEFT JOIN is generally much easier to understand than RIGHT JOIN ("Keep all rows in the first table" versus "keep all rows in the last table, which I haven't read yet"). However, this query probably only needs an INNER JOIN.
  • Presumably, Warehouses should be the first table, because your question is about this entity.
  • The HAVING clause does the comparison after the aggregation.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

join count from one table to select from another - mysql

From Dev

Select from one table and count from another

From Dev

MYSQL select from table and count from another

From Dev

MySQL project design - conditionally select from one table based on rows from another select query

From Dev

select from one table, count from another where id is not linked

From Java

SQL query INSERT SELECT COUNT from one table to another row by row

From Dev

MySQL select * from one table based on stock from another table

From Dev

MySQL count of count, use result from one table with another

From Dev

Select from another table taking the result from one query

From Dev

MYSQL join one colum from one table to two count() in another

From Dev

COUNT query with mysql with condition from one field from table

From Dev

select from one table, insert into another table oracle sql query

From Dev

SQL query to select row from one table which is not in another table

From Dev

PHP MySQL Select ID from one table and photo from another

From Dev

MySQL query with COUNT and join column from another table

From Dev

MYSQL query SELECT from one table and join results with other table

From Dev

MySQL Query Select from table count total and used, calculate remaining

From Dev

MySQL Query - SELECT row count from other table

From Dev

SQL Query - select all from one table with matching records in another

From Dev

MySQL: Return row count from second table using one query

From Dev

mySQL SELECT from one table, JOIN from another, then SELECT from the new one

From Dev

Selecting SUM+COUNT from one table and COUNT from another in Single query

From Dev

MySQL Advanced select query checking data from another table

From Dev

MySQL SELECT from one table and INSERT in another - Performance

From Dev

Mysql Query for inserting from one table to another on Multiple conditions and columns

From Dev

Mysql Query for inserting from one table to another on Multiple conditions and columns

From Dev

how to select a record from one table and count the number of which appears another table and count it

From Dev

MYSQL - Select twice from one column in multi table query

From Dev

MySQL Query from one table - Select same field twice

Related Related

  1. 1

    join count from one table to select from another - mysql

  2. 2

    Select from one table and count from another

  3. 3

    MYSQL select from table and count from another

  4. 4

    MySQL project design - conditionally select from one table based on rows from another select query

  5. 5

    select from one table, count from another where id is not linked

  6. 6

    SQL query INSERT SELECT COUNT from one table to another row by row

  7. 7

    MySQL select * from one table based on stock from another table

  8. 8

    MySQL count of count, use result from one table with another

  9. 9

    Select from another table taking the result from one query

  10. 10

    MYSQL join one colum from one table to two count() in another

  11. 11

    COUNT query with mysql with condition from one field from table

  12. 12

    select from one table, insert into another table oracle sql query

  13. 13

    SQL query to select row from one table which is not in another table

  14. 14

    PHP MySQL Select ID from one table and photo from another

  15. 15

    MySQL query with COUNT and join column from another table

  16. 16

    MYSQL query SELECT from one table and join results with other table

  17. 17

    MySQL Query Select from table count total and used, calculate remaining

  18. 18

    MySQL Query - SELECT row count from other table

  19. 19

    SQL Query - select all from one table with matching records in another

  20. 20

    MySQL: Return row count from second table using one query

  21. 21

    mySQL SELECT from one table, JOIN from another, then SELECT from the new one

  22. 22

    Selecting SUM+COUNT from one table and COUNT from another in Single query

  23. 23

    MySQL Advanced select query checking data from another table

  24. 24

    MySQL SELECT from one table and INSERT in another - Performance

  25. 25

    Mysql Query for inserting from one table to another on Multiple conditions and columns

  26. 26

    Mysql Query for inserting from one table to another on Multiple conditions and columns

  27. 27

    how to select a record from one table and count the number of which appears another table and count it

  28. 28

    MYSQL - Select twice from one column in multi table query

  29. 29

    MySQL Query from one table - Select same field twice

HotTag

Archive