This topic is locked

Query BETWEEN dates with user input

8/2/2024 7:08:04 AM
PHPRunner General questions
R
rfischerjr authorDevClub member

How can I implement this with user in put for start_date and end_date:

SELECT
vendor_name,
date_added_to_db AS start_date,
date_added_to_db AS end_date,
SUM(taxable_total) AS totalprice
FROM aq_contract_estimate
WHERE (date_added_to_db BETWEEN start_date AND end_date)
GROUP BY vendor_name WITH ROLLUP

thank you!

Sergey Kornilov admin 8/2/2024

I think the easiest option to achieve this is to use SQL variables. You can get those dates from the user, save them to session variables and then your SQL query may look like this:

SELECT
vendor_name,
date_added_to_db AS start_date,
date_added_to_db AS end_date,
SUM(taxable_total) AS totalprice
FROM aq_contract_estimate
WHERE (date_added_to_db BETWEEN ':session.start_date' AND ':session.end_date')
GROUP BY vendor_name WITH ROLLUP