改进MySQL Query以获取记录

时间:2014-07-07 17:06:09

标签: mysql sql

我有一个名为progress的表,它为客户和成员存储不同类型的进度;

enter image description here

我有一个MYSQL查询,它假设从客户端到成员和成员到客户端获取进度数据。目前我有以下方式查询;

SELECT * 
FROM progress
WHERE (client = 'XXXX' AND member = 'YYYY') 
    OR (client = 'YYYY' AND member = 'XXXX') 

它为我提供了我想要的结果,但是如何根据其编写方式改进此查询。

请帮助,非常感谢任何帮助。

提前谢谢

2 个答案:

答案 0 :(得分:2)

正如Barmar在评论It can't get much simpler中所说,但根据你写这个问题的问题,你有一些选择:

您可以使用UNION

select * from progress where (client = 'xxxx' and member = 'yyyy') 
 union all
select * from progress where (client = 'yyyy' and member = 'xxxx')

您可以使用IN语句

select * from progress
 where (client) in ('XXXX','YYYY')
   and (member) in ('YYYY','XXXX');

您可以使用自己的解决方案。

修改

忘记添加小提琴:http://sqlfiddle.com/#!2/74e6ec/7

答案 1 :(得分:0)

尝试使用联盟。

SELECT * FROM progress WHERE (client = 'XXXX' AND member = 'YYYY') 
UNION ALL
SELECT * FROM progress WHERE (client = 'YYYY' AND member = 'XXXX')