如何返回mysql子查询的行数

时间:2011-05-26 20:09:00

标签: mysql sql subquery

我有以下SQL查询:

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

这对我的使用来说不太正确

我想要的是:

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
    )

即。仅在子查询检索多于一行时选择。

2 个答案:

答案 0 :(得分:5)

就是这样:

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

答案 1 :(得分:2)

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)