Saad A
Saad A

Reputation: 1147

Improve MySQL Query to fetch records

I have table called progress which stores different type of progress for both client and member;

enter image description here

I have a MYSQL Query, which is suppose to get progress data both from the client to member and member to client. Currently I have a queries in the following way;

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

it gets me the result I want, but how can I improve this query in terms of the way it is written.

Please help, any help is much appreciated.

Thank you in advance

Upvotes: 0

Views: 38

Answers (2)

Jorge Campos
Jorge Campos

Reputation: 23371

As Barmar said in the comments It can't get much simpler but you have some options as per your question of WRITING this query:

You can use a UNION

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

You can use IN statements

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

And you can use your own solution.

Edit

Forgot to add the fiddle i did: http://sqlfiddle.com/#!2/74e6ec/7

Upvotes: 2

go0n
go0n

Reputation: 108

Try using a Union.

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

Upvotes: 0

Related Questions