I have a database with options of setting watermark images. If the client is not found, it should return the default watermark image name from the table. The data is as follows:
Table name: watermark
id | watermark_image_name | use_watermark | client
1 default_watermark.png | 1 | NULL
2 client1.png | 1 | client1
So if for example, client = client1, it'll only return client1.png. If client is null, or I specify client2 which doesn't exist, it'll return the default value of default_watermark.png from the table.
Cheers
Here is one way to do this via a union/limit trick:
(SELECT * FROM watermark WHERE client IS NULL)
UNION ALL
(SELECT * FROM watermark WHERE client = 'client1')
ORDER BY client DESC
LIMIT 1
This trick works by generating an intermediate result set containing the null client row, along with, possibly, the matching client record. We sort descending by client, to force the null row into the second position, should a matching client be found. If no match be found, then we just return the default null row.