Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

mysql if exists return the record value instead of regular output

I want a select that if/exists it returns the ‘link’ value instead of regular output.
so instead of ‘1’ it returns the ‘link’

Is that possible?

SELECT IF(
    EXISTS(
        SELECT link FROM modules WHERE module='license'
    ), 1, '/dashboard/'
)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use aggregation with MAX() (or MIN()):

SELECT COALESCE(MAX(link), '/dashboard/') link
FROM modules 
WHERE module = 'license';

If that specific link does not exist in modules then MAX() will return null in which case COALESCE() will return '/dashboard/'.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading