简体   繁体   English

超过1个OR的mysql SELECT

[英]mysql SELECT with more than 1 OR's

I am trying to make a numrows query to see if the user is friends with the other user, by doing user1 and user2. 我正在尝试通过执行user1和user2进行一次数字查询,以查看该用户是否与另一个用户成为朋友。

Here is my query, but this doesn't work. 这是我的查询,但这不起作用。

SELECT * FROM friends 
WHERE friend1 = USER_ID
AND friend2 = FRIEND
OR
WHERE friend1 = FRIEND
AND friend2 = USER_ID

So the query is checking to see it in both ways, back to front, how do I make this possible. 因此,该查询正在从头到尾两种方式进行检查,以使之成为可能。

You can only have one WHERE in a SQL statement, so you would want to use something like: SQL语句中只能有一个WHERE ,因此您需要使用类似以下的内容:

SELECT * 
FROM friends 
WHERE 
(
  friend1 = USER_ID
  AND friend2 = FRIEND
) 
OR
(
  friend1 = FRIEND
  AND friend2 = USER_ID
) 

The parentheses are not required due to operator precedence, I used them for clarity. 由于运算符优先级,不需要括号,为了清楚起见,我使用了括号。

您是否尝试使用括号?

SELECT * FROM friends WHERE (friend1 = USER_ID AND friend2 = FRIEND) OR (WHERE friend1 = FRIEND AND friend2 = USER_ID) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM