I have a query with subquery in it. the subquery returns the value, that i need to return in php and that also is used in "where" clause. i trying to figure out how can i not exequte th subquery two times. I trying to assign the value of it to the variable. And it works fine in "select", but when i use variable in "where" clause, the query returns 0 rows.
SELECT t.tour_id, t.tour_name, u.company_name, u.first_name, u.last_name,
@expireDate:= (SELECT DATE_ADD(tour_start_date, INTERVAL (t.tour_duration - 1) DAY)
FROM travelhub_tours_instance
WHERE tour_id = t.tour_id
ORDER BY tour_start_date DESC
LIMIT 1) AS expire,
( @expireDate + INTERVAL 14 DAY ) AS expirediff,
CURDATE() AS now,
( (@expireDate + INTERVAL 14 DAY) = CURDATE() ) AS criteria
FROM travelhub_tours t
JOIN travelhub_users u ON t.operator_id = u.user_id
WHERE (@expireDate + INTERVAL 14 DAY) = CURDATE()
In the WHERE clause, I put the same as in "criteria" column. and without the WHERE clause it variable work exactly how I expect. I'm confused - without "where":