使用“ ND使用运算符的各种条件MySQL从单表返回精确匹配的行
技术问答
344 人阅读
|
0 人回复
|
2023-09-14
|
select
+ ~5 q$ J# A( L games_exchange.u_exchange_id
3 x) x2 V% N0 `( i# Z3 G- c7 J' b from
: V; {0 x c( x7 p5 }2 t games_exchange
`1 W* V3 Q' U3 k where1 g4 s! K) z6 n2 n2 a( a
( games_exchange.game_id = 7
& f# U f! |3 I1 r2 M& ?# m1 D AND games_exchange.exchange_type = 1 )/ M. m' J" `! P+ H/ B. T$ Q
AND (
4 J1 X" C) ]# D* C( m( Y games_exchange.game_id = 7 1 y! j9 i8 B7 r* C) s6 A: t
AND games_exchange.exchange_type = 2 )
3 J i/ S5 a& z2 L6 J group by
# N4 _7 S$ K1 A games_exchange.u_exchange_id
0 \9 y, h9 }* X/ E! _- o HAVING
2 w8 m1 U: ?# h' s c2 X1 B COUNT( games_exchange.u_exchange_id ) = 2( V) A& B, M" v/ x! M& d
我从上述查询中得到的预期结果是u_exchange_id =171.但上述查询已返回。u_exchange_id = 171&170。
" g& M2 ~) k# r" p: x: @; J+ o 8 X/ x, r+ W) R- w5 `/ V' H
解决方案: ' n) U. ?' ] I- m2 S2 X5 J
经过大量的研究,我想出了解决我问题的办法。事实上,我使用有条件句子的错误方法where条件。下面的代码给出了解决我问题的建议
7 {* T: |( b7 ]5 LSELECT 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注意:我在两部分玩游戏ID上使用foreach()为每个人实现和实现ID动态添加子查询。 |
|
|
|
|
|