使用 "AND" 运算符的多个条件和使用 MySQL 来自单个 table 的 return 精确匹配行
Multiple condition using "AND" operator and return exact matching rows from a single table using MySQL
select
games_exchange.u_exchange_id
from
games_exchange
where
( games_exchange.game_id = 7
AND games_exchange.exchange_type = 1 )
AND (
games_exchange.game_id = 7
AND games_exchange.exchange_type = 2 )
group by
games_exchange.u_exchange_id
HAVING
COUNT( games_exchange.u_exchange_id ) = 2
我对上述查询的预期结果是 u_exchange_id = 171。
但是上面的查询 return u_exchange_id = 171 & 170.
[table 的屏幕截图在此处][2]
经过大量研究,我找到了解决问题的办法。实际上,我在使用 having 子句时错误地使用了 where 条件。
针对我的问题提出的解决方案在下面的代码中给出
SELECT u_exchange_id FROM games_exchange
WHERE
u_exchange_id IN
( SELECT u_exchange_id FROM games_exchange WHERE game_id = 7 AND
exchange_type = 1 )
AND
u_exchange_id IN
( SELECT u_exchange_id FROM games_exchange WHERE game_id = 7 AND
exchange_type = 2)
GROUP BY u_exchange_id
HAVING COUNT( u_exchange_id ) = 2
Note: I achieved this by using foreach() on games ids of both section and I added dynamically sub-query for every id.
select
games_exchange.u_exchange_id
from
games_exchange
where
( games_exchange.game_id = 7
AND games_exchange.exchange_type = 1 )
AND (
games_exchange.game_id = 7
AND games_exchange.exchange_type = 2 )
group by
games_exchange.u_exchange_id
HAVING
COUNT( games_exchange.u_exchange_id ) = 2
我对上述查询的预期结果是 u_exchange_id = 171。 但是上面的查询 return u_exchange_id = 171 & 170.
[table 的屏幕截图在此处][2]
经过大量研究,我找到了解决问题的办法。实际上,我在使用 having 子句时错误地使用了 where 条件。 针对我的问题提出的解决方案在下面的代码中给出
SELECT u_exchange_id FROM games_exchange
WHERE
u_exchange_id IN
( SELECT u_exchange_id FROM games_exchange WHERE game_id = 7 AND
exchange_type = 1 )
AND
u_exchange_id IN
( SELECT u_exchange_id FROM games_exchange WHERE game_id = 7 AND
exchange_type = 2)
GROUP BY u_exchange_id
HAVING COUNT( u_exchange_id ) = 2
Note: I achieved this by using foreach() on games ids of both section and I added dynamically sub-query for every id.