如何获取具有给定日期值的varchar date列

时间:2018-07-03 09:31:45

标签: php mysql sql ajax mysqli

我有一张桌子。其中,列名是tb_date,它是varchar格式。在该列中保存日期,但格式不同(例如:01/07/201或01-08-2018或2017/03/12等)。现在我要在给定日期之间进行搜索。但这是行不通的。我试过了-

$result=mysql_query("select * from user_History") or die(mysql_error());
while($row=mysql_fetch_array($result))
{
extract($row);
$time = strtotime( $tb_date );
$myDate = date( 'd/m/y', $time );

$result1=mysql_query("select * from user_History where '$myDate' BETWEEN '01/06/2018' AND '31/06/2018'") or die(mysql_error()); 
while($row1=mysql_fetch_array($result1))
{
    extract($row1);

    echo $tb_date;
    }                   
    }

但是它正在提供所有记录。

2 个答案:

答案 0 :(得分:0)

假定您在Mysql中将日期值类型设置为DATE,则应在Sql语句中以YYYY-MM-DD的格式给出日期。

更改

select * from user_History where '$myDate' BETWEEN '01/06/2018' AND '31/06/2018'

收件人

select * from user_History where `[DATE_COLUMN_NAME]` BETWEEN '2018-06-01' AND '2018-06-31'

希望这会有所帮助。

答案 1 :(得分:0)

您可以使用此IF语句转换此日期格式,但这不是最好的主意。更好的方法是对该字段进行规范化:

IF( tb_date REGEXP '^[0-9]{4}/[0-9]{2}/[0-9]{2}$', str_to_date(tb_date,'%Y/%m/%d'),
IF( tb_date REGEXP '^[0-9]{2}/[0-9]{2}/[0-9]{4}$', str_to_date(tb_date,'%d/%m/%Y'),
IF( tb_date REGEXP '^[0-9]{2}-[0-9]{2}-[0-9]{4}$', str_to_date(tb_date,'%d-%m-%Y') , 'ERROR'))) tb_date_new

样本

MariaDB [bernd]> SELECT * from user_History;
+----+------------+------------+
| id | tb_date    | some-field |
+----+------------+------------+
|  1 | 01/07/2014 | Line 1     |
|  2 | 01-08-2018 | Line 2     |
|  3 | 2017/03/12 | Line 3     |
+----+------------+------------+
3 rows in set (0.00 sec)


MariaDB [bernd]> SELECT u.*,
    -> IF( tb_date REGEXP '^[0-9]{4}/[0-9]{2}/[0-9]{2}$', str_to_date(tb_date,'%Y/%m/%d'),
    -> IF( tb_date REGEXP '^[0-9]{2}/[0-9]{2}/[0-9]{4}$', str_to_date(tb_date,'%d/%m/%Y'),
    -> IF( tb_date REGEXP '^[0-9]{2}-[0-9]{2}-[0-9]{4}$', str_to_date(tb_date,'%d-%m-%Y') , 'ERROR'))) tb_date_new
    -> FROM user_History u
    -> ;
+----+------------+------------+-------------+
| id | tb_date    | some-field | tb_date_new |
+----+------------+------------+-------------+
|  1 | 01/07/2014 | Line 1     | 2014-07-01  |
|  2 | 01-08-2018 | Line 2     | 2018-08-01  |
|  3 | 2017/03/12 | Line 3     | 2017-03-12  |
+----+------------+------------+-------------+
3 rows in set (0.00 sec)

MariaDB [bernd]> SELECT u.*,
    -> IF( tb_date REGEXP '^[0-9]{4}/[0-9]{2}/[0-9]{2}$', str_to_date(tb_date,'%Y/%m/%d'),
    -> IF( tb_date REGEXP '^[0-9]{2}/[0-9]{2}/[0-9]{4}$', str_to_date(tb_date,'%d/%m/%Y'),
    -> IF( tb_date REGEXP '^[0-9]{2}-[0-9]{2}-[0-9]{4}$', str_to_date(tb_date,'%d-%m-%Y') , 'ERROR'))) tb_date_new
    -> FROM user_History u
    ->
    -> WHERE
    -> IF( tb_date REGEXP '^[0-9]{4}/[0-9]{2}/[0-9]{2}$', str_to_date(tb_date,'%Y/%m/%d'),
    -> IF( tb_date REGEXP '^[0-9]{2}/[0-9]{2}/[0-9]{4}$', str_to_date(tb_date,'%d/%m/%Y'),
    -> IF( tb_date REGEXP '^[0-9]{2}-[0-9]{2}-[0-9]{4}$', str_to_date(tb_date,'%d-%m-%Y') , 'ERROR')))
    -> BETWEEN str_to_date('01/06/2018','%d/%m/%Y') AND str_to_date('31/08/2018','%d/%m/%Y');
+----+------------+------------+-------------+
| id | tb_date    | some-field | tb_date_new |
+----+------------+------------+-------------+
|  2 | 01-08-2018 | Line 2     | 2018-08-01  |
+----+------------+------------+-------------+
1 row in set (0.00 sec)

MariaDB [bernd]>