I have a table like this:
projectName | info
--------------------
all | i1
all | i2
all | i3
name1 | i4
name1 | i5
all | i6
I have a query that checks the project name. If it exists in the table, I have to select only the information regarding that specific project. If it does not exist, I must get the information for 'all' projects.
For example, if I my entry is 'name1', my output should be:
i4
i5
If my entry is 'name2', my output should be:
i1
i2
i3
i6
Is there a way I can do this in a mysql query? I looked for examples but everything I found was about retrieving information from two different tables.
One way is to use UNION ALL
:
SELECT info
FROM mytable
WHERE projectName = 'name1'
UNION ALL
SELECT info
FROM mytable
WHERE projectName = 'all' AND
NOT EXISTS (SELECT 1
FROM mytable
WHERE projectName = 'name1')
Demo here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With