Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to return row count of mysql sub query

I have the following SQL query:

SELECT games.id, games.GameTitle FROM games 
WHERE EXISTS (
              SELECT filename FROM banners 
              WHERE banners.keyvalue = games.id 
                AND banners.filename LIKE '%front%'
             )

which is not quite correct for my use

what I'd like is something like:

SELECT games.id, games.GameTitle 
FROM games WHERE EXISTS (
    COUNT(SELECT filename FROM banners 
    WHERE banners.keyvalue = games.id AND banners.filename LIKE '%front%') > 1
    )

i.e. only select when the subquery retrieves more than 1 row.

like image 738
Alex Avatar asked May 26 '11 20:05

Alex


People also ask

How do I count rows in SQL subquery?

SELECT FUCNTIONIMLOOKINGFOR(SELECT * FROM anothertable) AS count FROM table; So that count is an integer of how many rows the subquery SELECT * FROM anothertable returns.

How do I return the number of rows in MySQL?

The COUNT(*) function returns the number of rows in a result set returned by a SELECT statement. The COUNT(*) returns the number of rows including duplicate, non-NULL and NULL rows.

How do I get row count in SQL SELECT query?

To counts all of the rows in a table, whether they contain NULL values or not, use COUNT(*). That form of the COUNT() function basically returns the number of rows in a result set returned by a SELECT statement.

How do you get the row count?

Just click the column header. The status bar, in the lower-right corner of your Excel window, will tell you the row count. Do the same thing to count columns, but this time click the row selector at the left end of the row. If you select an entire row or column, Excel counts just the cells that contain data.


2 Answers

Simply like that :

SELECT games.id, games.GameTitle 
FROM games 
WHERE (
    SELECT COUNT(filename)
    FROM banners
    WHERE banners.keyvalue = games.id AND banners.filename LIKE '%front%'
) > 1
like image 99
krtek Avatar answered Nov 09 '22 01:11

krtek


SELECT games.id, games.GameTitle 
    FROM games 
    WHERE EXISTS (SELECT COUNT(filename) 
                      FROM banners 
                      WHERE banners.keyvalue = games.id 
                          AND banners.filename LIKE '%front%'
                      HAVING COUNT(filename)>1)
like image 31
Joe Stefanelli Avatar answered Nov 09 '22 01:11

Joe Stefanelli