So I have the following two sql statements that I would like to combine into one.
SELECT `client_id` FROM `accommodation` WHERE `checkin` >= from AND `checkout` <= to
SELECT `client_id` FROM `conference` WHERE `checkin` >= from AND `checkout` <= to
So basically the two sql statments fetch data from two tables based on the checkin and checkout dates.
What is the best possible way of combining the two statements so as to get all client_ids that meet the criteria
Presumably, by "meet the criteria" you mean that both conditions are met. That suggests a join
. So try this:
SELECT a.`client_id`
FROM `accommodation` a join
conference c
on a.client_id = c.client_id
WHERE a.checkin >= from AND a.checkout <= to and
c.checkin >= from AND c.checkout <= to;
Of course, reserved words such as from
should be avoided as column or variable names. If you feel you must use one, be sure to escape it.