Reputation: 753
I have a table with fruits:
id fruit_type color
--------------------------
1 apple yellow
2 apple green
3 strawberry red
I want to select all fruits from my table. But if it's an apple, I only want to include it if it's green. How can I achieve that in a single MySQL query?
Upvotes: 1
Views: 58
Reputation: 20315
Nothing complicated, you just have to use a OR
operator in your WHERE
condition:
SELECT *
FROM fruits
WHERE fruit_type != "apple"
OR color = "green"; /* ie where fruit_type = "apple" and color = "green" */
.
Upvotes: 2
Reputation: 1247
here i create query for sql server
. same way you can create in mysql
Select id, fruit_type,color from
(
Select
Case when fruit_type = 'apple' and color <> 'Green'
then 'N' Else 'Y' end as selected,
*
from fruits )temptable
where temptable.selected = 'Y'
Upvotes: 0
Reputation: 3591
I think you could use union.
Select * from fruits where fruit_type<> 'apple'
Union
Select * from fruits where fruit_type= 'apple' and color = 'green'
Upvotes: 0