1

I have a below query that returns a result. How can query the result which if there is result, return True, and if there is no result, return FALSE??

SELECT a.id 
FROM table1 AS a 
INNER JOIN table2 AS c
   ON (a.id=c.user_id AND a.id=4 AND c.complete_date is NULL AND c.st_number= 8)
Luuk
  • 12,245
  • 5
  • 22
  • 33
Freddy
  • 91
  • 1
  • 10
  • I think you will find your answer [here](https://stackoverflow.com/questions/14342535/is-it-possible-for-a-mysql-query-to-return-true-false-instead-of-values). Possible duplicate – chized Apr 18 '22 at 09:34
  • @chized: indeed possible duplicatie, but solution from Tim B. below is much better – Luuk Apr 18 '22 at 09:35

2 Answers2

2

You could use EXISTS here:

SELECT EXISTS (
    SELECT a.id
    FROM table1 AS a
    INNER JOIN table2 AS c ON a.id = c.user_id
    WHERE a.id = 4 AND c.complete_date IS NULL AND c.st_number = 8
);
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Thanks Tim, worked perfect. I was keep trying SELECT EXISTS but it didn't work for me. cheers – Freddy Apr 18 '22 at 10:24
0

Try this,

Please don't merge inner join and where condition

SELECT a.id FROM table1 AS a 
INNER JOIN table2 AS c ON a.id=c.user_id 
where a.id=4 AND c.complete_date is NULL AND c.st_number= 8
  • This will return a list of values (the values from `a.id`) How can this be "return True, and if there is no result, return FALSE" ? – Luuk Apr 18 '22 at 09:38