例如我要求:
WHERE (friend_id=? OR client_id=?)
我如何知道哪一行符合friend_id条件且哪些符合client_id条件?
是否有可能以某种方式根据会议条件标记/标记行?
感谢。
答案 0 :(得分:2)
SELECT friend_id=? FROM yourtable WHERE (friend_id=? OR client_id=?);
如果friend_id子句匹配,您将获得true: 甚至:
SELECT friend_id=?, client_id=? FROM yourtable WHERE (friend_id=? OR client_id=?);
获得两场比赛。通过这种方式,您可以看到一个或两个匹配。
答案 1 :(得分:1)
如果因任何条件知道哪一行被点击,您当然可以使用case
运算符将此数据添加到结果列中。唯一的缺点是你准备好的陈述中的变量(如果这就是你在这里所拥有的)将会加倍。
答案 2 :(得分:0)
您可以使用UNION。
例如:
SELECT name, 1
FROM friends
WHERE friend_id=?
UNION
SELECT name, 0
FROM friends
WHERE client_id=?
然后在接收数据时,您可以检查该标志
答案 3 :(得分:0)
SELECT *,
'Friend' AS Source
FROM TABLE
WHERE friend_id = ?
UNION
SELECT *,
'Client' AS Source
FROM TABLE
WHERE client_id = ?
但如果您的参赛作品是朋友和客户,那么您将遇到问题。在这种情况下你想发生什么?
答案 4 :(得分:0)
你可以使用IF(),但是你必须绑定一个id两次:
SELECT IF(friend_id=?, 'friend', 'client') AS type
FROM table
WHERE (friend_id=? OR client_id=?)
答案 5 :(得分:0)
您可以在查询中添加标记:
SELECT *, IF((friend_id=$friend_id), 1, 0) AS friend_matched, IF((client_id=$client_id), 1, 0) AS client_matched FROM table WHERE (friend_id=? OR client_id=?)