使用值

时间:2016-07-01 14:55:38

标签: mysql sql

我的查询有问题。

结果集:

enter image description here

SELECT players.codsw,sessions.userid,sessions.idlocation,location.denomination
from sessions 
LEFT JOIN location ON location.idlocation = sessions.idlocation
LEFT JOIN players ON players.userid = sessions.userid
GROUP BY sessions.userid,sessions.idlocation
    HAVING players.codsw IN ('MEGA SHOW','ANCORINA')
ORDER BY location.denomination

我需要仅提取同时包含codsw的记录,在本例中为MEGA SHOW和ANCORINA以及idlocation

如果您看到我的结果集,则会显示所有idlocation的第一个或第二个codsw,而不是两个。

我希望结果只能看到例如idlocation 34或623而不是另一个。

非常感谢

的Riccardo

2 个答案:

答案 0 :(得分:0)

以下查询中的子查询使用条件聚合来标识包含MEGA SHOWANCORINA的所需组。然后,此子查询将内部连接到您的原始查询,以过滤掉您不希望出现在结果集中的记录。

SELECT players.codsw, sessions.userid, sessions.idlocation, location.denomination
FROM sessions 
LEFT JOIN location
    ON location.idlocation = sessions.idlocation
LEFT JOIN players
    ON players.userid = sessions.userid
INNER JOIN
(
    SELECT sessions.userid AS userid, sessions.idlocation AS idlocation,
           SUM(CASE WHEN players.codsw IN ('MEGA SHOW', 'ANCORINA')
                    THEN 1 ELSE 0 END) AS codswSum
    FROM sessions 
    LEFT JOIN location
        ON location.idlocation = sessions.idlocation
    LEFT JOIN players
        ON players.userid = sessions.userid
    GROUP BY sessions.userid, sessions.idlocation
    HAVING codswSum = 2
) t
    ON sessions.userid = t.userid AND sessions.idlocation = t.idlocation
ORDER BY location.denomination

答案 1 :(得分:0)

试试这个:

A = [1111,2222,3333]
B = [20,25,26,27]
C = [1.5,2.6,3.7,4.5,5.4,6.7,7.2,8.8,9.0,10.0,11.0,12.2]