Mysql 提取具有两个值的记录
我有一个疑问 结果集: 我只需要同时使用Mysql 提取具有两个值的记录,mysql,sql,Mysql,Sql,我有一个疑问 结果集: 我只需要同时使用codsw,在本例中,MEGA SHOW和ANCORINA以及相同的idlocation,来提取记录 如果您看到我的结果集,它会显示所有带有第一个或第二个codsw的idlocation,而不是两者 例如,我希望结果只看到idlocation34或623,而不是另一个 非常感谢 Riccardo以下查询中的子查询使用条件聚合来标识包含MEGA SHOW和ANCORINA的所需组。然后,此子查询与原始查询进行内部联接,以过滤掉不希望出现在结果集中的记录 S
codsw
,在本例中,MEGA SHOW和ANCORINA以及相同的idlocation
,来提取记录
如果您看到我的结果集,它会显示所有带有第一个或第二个codsw
的idlocation
,而不是两者
例如,我希望结果只看到idlocation
34或623,而不是另一个
非常感谢
Riccardo以下查询中的子查询使用条件聚合来标识包含
MEGA SHOW
和ANCORINA
的所需组。然后,此子查询与原始查询进行内部联接,以过滤掉不希望出现在结果集中的记录
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
以下查询中的子查询使用条件聚合来标识同时包含
MEGA SHOW
和ANCORINA
的所需组。然后,此子查询与原始查询进行内部联接,以过滤掉不希望出现在结果集中的记录
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
试试这个:
SELECT players.codsw
,sessions.userid
,sessions.idlocation
,location.denomination
FROM sessions sess
,location loc
,players pl
WHERE location.idlocation = sessions.idlocation
AND players.userid = sessions.userid
AND players.codsw IN ('MEGA SHOW', 'ANCORINA')
AND EXISTS (SELECT sessions.idlocation
,COUNT(*)
FROM sessions
,location
,players
WHERE location.idlocation = sessions.idlocation
AND players.userid = sessions.userid
AND players.codsw IN ('MEGA SHOW', 'ANCORINA')
AND sessions.idlocation = sess.idlocation
GROUP BY sessions.idlocation
HAVING COUNT(*) > 1)
ORDER BY location.denomination
试试这个:
SELECT players.codsw
,sessions.userid
,sessions.idlocation
,location.denomination
FROM sessions sess
,location loc
,players pl
WHERE location.idlocation = sessions.idlocation
AND players.userid = sessions.userid
AND players.codsw IN ('MEGA SHOW', 'ANCORINA')
AND EXISTS (SELECT sessions.idlocation
,COUNT(*)
FROM sessions
,location
,players
WHERE location.idlocation = sessions.idlocation
AND players.userid = sessions.userid
AND players.codsw IN ('MEGA SHOW', 'ANCORINA')
AND sessions.idlocation = sess.idlocation
GROUP BY sessions.idlocation
HAVING COUNT(*) > 1)
ORDER BY location.denomination
谢谢的Tim,但没有任何更改会产生效果,我总是使用单idlocation和单codsw,如idlocation=4058。此查询将自动实现您的逻辑。如果您能给出具体的反馈,说明哪些地方不起作用,也许我可以提供帮助。我必须使用codesw MEGA SHOW和ANCORINA提取所有位置,查询实际上提取例如idlocation=4058,其中只有MEGA SHOW记录而没有ANCORINA。如果没有MEGA SHOW和ANCORINA,我就不必退出记录。你确定你真的测试了我的查询吗?您确定
idlocation
4058没有codsw
ANCORINA
?是的,Tim,结果只有一个记录MEGA SHOW 18532 3729 Magazzino:MGZ Games Lodi Pordenone 2银行的Tim,但没有任何更改生效,我总是使用单idlocation和单codsw,如idlocation=4058。此查询将自动实现您的逻辑。如果您能给出具体的反馈,说明哪些地方不起作用,也许我可以提供帮助。我必须使用codesw MEGA SHOW和ANCORINA提取所有位置,查询实际上提取例如idlocation=4058,其中只有MEGA SHOW记录而没有ANCORINA。如果没有MEGA SHOW和ANCORINA,我就不必退出记录。你确定你真的测试了我的查询吗?你确定idlocation
4058没有codsw
ANCORINA
吗?是的,蒂姆,结果只有一个records MEGA SHOW 18532 3729 Magazzino:MGZ Games Lodi Pordenone 2是的,执行查询和结果需要很多时间设置它非常大,但这就像我的期望一样,执行查询和结果集需要很多时间,它非常大,但我的期望是什么