Filter by count from another table

holden321

This query works fine only without WHERE, otherwise there is an error:

column "cnt" does not exist

SELECT
  *,
  (SELECT count(*)
   FROM B
   WHERE A.id = B.id) AS cnt
FROM A
WHERE cnt > 0
Gordon Linoff

Use a subquery:

SELECT a.*
FROM (SELECT A.*,
             (SELECT count(*)
              FROM B
              WHERE A.id = B.id
             ) AS cnt
      FROM A
     ) a
WHERE cnt > 0;

Column aliases defined in the SELECT cannot be used by the WHERE (or other clauses) for that SELECT.

Or, if the id on a is unique, you can more simply do:

SELECT a.*, COUNT(B.id)
FROM A LEFT JOIN
     B
     ON A.id = B.id
GROUP BY A.id
HAVING COUNT(B.id) > 0;

Or, if you don't really need the count, then:

select a.*
from a
where exists (select 1 from b where b.id = a.id);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Count Row from another table

From Dev

Get count from another table

From Java

How to fast return and count rows from one table based on filter by two another tables

From Dev

Count from another table in FROM subquery

From Dev

Select from one table and count from another

From Dev

MYSQL select from table and count from another

From Dev

How to count rows from another table to affect another table

From Dev

mysql - count from different table supplemented with another count from subresult

From Dev

Join a columns count from another table

From Dev

SQL query with count() from another table

From Dev

Adding a count from another table to existing query

From Dev

How use count for value from another table

From Dev

SQLite select and count from another table with like

From Dev

MySQL - Join & Count rows from another table

From Dev

How to count data from another table

From Dev

sql - show count of field from another table

From Dev

Select * as well as count/sum from another table

From Dev

SQL COUNT Rows from another table

From Dev

Use Linq to filter data from another table

From Dev

SQL Server : filter on dates from another table

From Dev

Mysql Filter Data by Range from Another Table

From Dev

Selecting records from a table and then selecting a count of records from another table

From Dev

get th count from table and details from another table

From Dev

Display the details from one table and count from another table

From Dev

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

From Dev

SQL COUNT to count all the numbers from another table

From Dev

SQL COUNT to count all the numbers from another table

From Dev

Query within another query to count amount of items from another table

From Dev

Column loop and update another column with COUNT() value from another table

Related Related

  1. 1

    Count Row from another table

  2. 2

    Get count from another table

  3. 3

    How to fast return and count rows from one table based on filter by two another tables

  4. 4

    Count from another table in FROM subquery

  5. 5

    Select from one table and count from another

  6. 6

    MYSQL select from table and count from another

  7. 7

    How to count rows from another table to affect another table

  8. 8

    mysql - count from different table supplemented with another count from subresult

  9. 9

    Join a columns count from another table

  10. 10

    SQL query with count() from another table

  11. 11

    Adding a count from another table to existing query

  12. 12

    How use count for value from another table

  13. 13

    SQLite select and count from another table with like

  14. 14

    MySQL - Join & Count rows from another table

  15. 15

    How to count data from another table

  16. 16

    sql - show count of field from another table

  17. 17

    Select * as well as count/sum from another table

  18. 18

    SQL COUNT Rows from another table

  19. 19

    Use Linq to filter data from another table

  20. 20

    SQL Server : filter on dates from another table

  21. 21

    Mysql Filter Data by Range from Another Table

  22. 22

    Selecting records from a table and then selecting a count of records from another table

  23. 23

    get th count from table and details from another table

  24. 24

    Display the details from one table and count from another table

  25. 25

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

  26. 26

    SQL COUNT to count all the numbers from another table

  27. 27

    SQL COUNT to count all the numbers from another table

  28. 28

    Query within another query to count amount of items from another table

  29. 29

    Column loop and update another column with COUNT() value from another table

HotTag

Archive