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')
select * from projects
where projectName = case when exists (select * from projects where projectName = 'name1')
then 'name1'
else 'all'
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.