I have a complicated query I’ve been working on, and I recently changed one of the columns so that there will be multiple entries that reference the same foreign key.
Is there a simple way to make it so that if there are 1 or more entries with the same foreign key that a value of “true” (or some other value) is returned rather than n seperate rows?
eg.
I want this:
id | Date
1 | true
2 | NULL
instead of this (what is happening now):
id | Date
1 | feb 4
1 | feb 5
1 | feb 6
2 | NULL
2 | feb 5
if there are no values I just want a null for the date
EDIT
I forgot about the case where you have both values and nulls for the same primary key (id# 2). In this case I need to be able to return Null. If there are any nulls then it should return a null.
EDIT
I’ve tried playing around with a group by function but can’t get anything close to what I’m trying to accomplish
This is a simplified version of my query as it is already, don’t want to confuse the issue with the extra columns.
SELECT distinct job, DATE_FORMAT( scan_date, '%M %e, %Y, %l:%i%p' ) AS Scanning
FROM Scan, job
WHERE Scan.job_job = job
Check if any scan record exists rather than joining to the Scan table directly:
This will return 1 if a Scan record exists, 0 if not. If you really want to use “true” and NULL, you can wrap the exists in IF().