MySql If Exists Set Value?
mysql
Solution
I would look at `EXISTS` it is in most of the cases much faster then to `COUNT` all the items that matches your where statement. With that said. You query should look something like this:
SELECT
name,
poster,
sid,
(
CASE WHEN EXISTS(SELECT NULL FROM times WHERE shows.tid=times.tid)
THEN 1
ELSE 0
END
)AS tickets
FROM
shows
JOIN show_info ON (id) WHERE sid=54 order by name ASC
Problem
I have a query that returns a bunch of information and using a join to join two tables and it works perfectly fine. But I have a field called tickets which I need to see if there is a time available and if there is even one set it to 1 otherwise set it to 0. So like this. ``` SELECT name,poster,sid,tickets = (IF SELECT id FROM times WHERE shows.tid=times.tid LIMIT 1, if value returned set to 1, otherwise set to 0) FROM shows JOIN show_info ON (id) WHERE sid=54 order by name ASC ``` Obviously that is not a correct MySQL statement, but it would give an example of what I am looking for. Is this possible? Or do I need to do the first select then for a loop through results and do the second select and set value that way? Or is one better performance wise?