我有一个包含homeTeamId和visitorTeamId列的表。
我想显示homeTeamId = 39或43 AND visitorTeamId = 39或43
的行$sql ="SELECT * FROM game WHERE (homeTeamId='$_GET[homeTeamId]' or '$_GET[visitorTeamId]' and visitorTeamId='$_GET[homeTeamId]' or '$_GET[visitorTeamId]')";
给了我
homeTeamName visitorTeamName visitorTeamId homeTeamId visitorTeamScore homeTeamScore date
barrard teamName0 39 43 0 0 2016 01/17 09:41 pm
teamName0 barrard 43 39 0 0 2016 01/17 10:18 pm
teamName0 barrard 43 39 0 0 2016 01/17 10:26 pm
但是,如果我使用39和20之类的东西,我会得到完全相同的结果。
我试过这种方式
$sql ="SELECT * FROM game WHERE homeTeamId=('$_GET[homeTeamId]' or '$_GET[visitorTeamId]') and visitorTeamId=('$_GET[homeTeamId]' or '$_GET[visitorTeamId]')";
我没有结果,
感谢您的帮助。
答案 0 :(得分:0)
你应该尝试这样的事情:
$sql ="SELECT * FROM game WHERE (homeTeamId='$_GET[homeTeamId]' OR homeTeamId='$_GET[visitorTeamId]') AND (visitorTeamId='$_GET[homeTeamId]' OR visitorTeamId'$_GET[visitorTeamId]')";
或者使用较短的aproach IN,在您的帖子中发布评论! 使用IN,您可以实现更复杂的查询。
使用in:
$sql = "SELECT * FROM game WHERE homeTeamId IN ('$_GET[homeTeamId]',
'$_GET[visitorTeamId]') AND visitorTeamId IN ('$_GET[homeTeamId]',
'$_GET[visitorTeamId]')";
答案 1 :(得分:0)
试试这个:
$sql ="SELECT * FROM game WHERE
(homeTeamId='$_GET[homeTeamId]' OR homeTeamId='$_GET[visitorTeamId]')
AND
(visitorTeamId='$_GET[homeTeamId]' OR visitorTeamId='$_GET[visitorTeamId]')";
答案 2 :(得分:0)
MySQL运算符具有优先顺序。这意味着某些运营商在其他运营商之前进行评估。
我们可以使用括号来指定我们希望运算符评估的顺序,以覆盖默认顺序。
考虑:
SELECT '0 AND 1 OR 1' AS cond, 0 AND 1 OR 1
UNION ALL
SELECT '(0 AND 1) OR 1' AS cond, (0 AND 1) OR 1
UNION ALL
SELECT '0 AND (1 OR 1)' AS cond, 0 AND (1 OR 1)
返回:
cond 0 AND 1 OR 1
-------------- ------------
0 AND 1 OR 1 1
(0 AND 1) OR 1 1
0 AND (1 OR 1) 0