I have a database where i want to select data with date in between dates got from html form .
lets assume database has columns - custid(int)(5)
, query(varchar)(100)
, day(int)(2)
, month(int)(2)
, year(int)(2)
.
and i am getting date from form as dd/mm/yy
thereby i tried this but it's not working .
SELECT * FROM table
WHERE custid = 'x' AND
(CONCAT(day'./.'month'./.'year) AS DATE BETWEEN '01/01/01' AND '01/01/99');
// where 01/01/2001
is the starting date and 01/01/99
recieved from html form and x is the custid recieved from too).
Is there any way around to solve it ?
Error in sql :
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use
near '
month
'./.'year
) AS DATE BETWEEN '01/01/2001' AND '01/01/2099') LIMIT 0, 25' at line 3
Edit 1 :
After looking through concat() function documentation and answer by jens , sql query after updation.
SELECT * FROM table
WHERE custid = 'x' AND
(CONCAT(day','/','month','/','year) AS DATE BETWEEN '01/01/01' AND '01/01/99');
Your concat Syntax must by:
(CONCAT(day','/','month','/','year) BETWEEN '01/01/01' AND '01/01/99');
BTW: This makes a string compare not a date compare, so you should Change to a date format.
STR_TO_DATE(CONCAT('day','/','month','/','year'), '%d/%m/%Y') between STR_TO_DATE('01/01/01' ) and STR_TO_DATE('01/01/99', '%d/%m/%Y')
For more informations about the concat syntax see https://www.w3schools.com/sql/func_mysql_concat.asp and for str_to_date syntax https://www.w3schools.com/sql/func_mysql_str_to_date.asp
Some tips: don't use function or calculation at where clause with column.
In your case: 1. explode date from your form and get 3 part. 2. you can write SQL like this:
SELECT * FROM table
WHERE custid = 'x' AND
(year >= '1' AND year <='99' and month >= '1' and month <= '12' and day >= '1' and day <= '12')
Instead of comparing it as dates , i used concat() to use it as a numeric value and then using php functions i converted the date got fro mhtml form into numbers too, and then used simple between function to check if the value is in between the limits.
Then sql becomes
SELECT * FROM `ledger-company`
WHERE `companyid` = '10'
AND (CONCAT(year,month,day) BETWEEN '010101' AND '990199')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.