Click here to Skip to main content
16,004,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So, I want to get datas from database by filtering 4 columns. First column title is Money(amount of money) and the second is Currency, but I have in DB additionally two same Columns with same definitions for example: Money2 and Currency2. So I want to get all datas form db where Money is more than 100 and Currency is USD, but additionally I want to get all datas where Money2 is more than 200 and Currency2 is EUR. I have following query written but unfortunately it doesn't give result from second query.
SQL
SELECT * FROM DB WHERE (Money > 100 AND Currency =  USD) OR Money2 > 200 AND Currency2 = EUR). 
Where have i made a failure ?
Posted
Updated 20-Jan-16 13:23pm
v2
Comments
PIEBALDconsult 20-Jan-16 20:14pm    
Be _very_ careful when mixing AND with OR -- I recommend you add more parentheses to specify _exactly_ what you intend.

From your query(given below for reference), I can see two misses:
SELECT * FROM DB WHERE (Money > 100 AND Currency =  USD) OR Money2 > 200 AND Currency2 = EUR). 

First, the statement may be missing a open bracket "(" before Money2, so you can try adding that to the select query, like:
SELECT * FROM DB WHERE (Money > 100 AND Currency =  USD) OR (Money2 > 200 AND Currency2 = EUR) 

Second, from the data, I assume Currency and Currency2 are varchar or char fields. You might want to try by adding single quotes for these inputs, like:
SELECT * FROM DB WHERE (Money > 100 AND Currency =  'USD') OR (Money2 > 200 AND Currency2 = 'EUR'). 
 
Share this answer
 
Comments
_Asif_ 21-Jan-16 5:17am    
+5
You dont give us any examples of the data in your DB, or say what the 'failure' actually is

I'd hazard one guess/hint, the 'OR Money2...' clause should be 'AND Money2.....' - you say
Quote:
additionally I want to
which means you want both sets of data returned, not one or the other as you have now ..

without seeing your data I'd also be thinking wether a 'union' is what you need, what if you have data that satisfies both contraints in the same record - Ideally you would want 2 lines of data out ? yes/no/maybe ?
 
Share this answer
 
Solution for this question is to use UNION like this : SELECT * FROM DB WHERE Money > 100 AND Currency = 'USD' UNION Money2 > 200 AND Currency = 'EUR'
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900