简体   繁体   English

在ProductSell模型中,日期列类型为varchar,格式为d / m / Y,如何使用两者之间的数据来检索数据?

[英]In ProductSell model there is a date column type is varchar and format d/m/Y how can I retrieve data using wherebetween?

I have a model ProductSell with a column date which type is varchar and format is d/m/Y . 我有一个型号为ProductSell的列日期,其类型为varchar,格式为d / m / Y。

Suppose I have two date 31/03/2019 and 01/04/2019 . 假设我有两个日期31/03/2019和01/04/2019。 I want to fetch data between these dates I am trying like 我想在这些日期之间获取数据,例如

$productsale=ProductSell::whereBetween('date',[$request->from,$request- 
>to])->get();

when date is 31/03/2019 and 01/04/2019 this is not working but when date is 01/03/2019 and 31/03/2019 it working fine 当日期是31/03/2019和01/04/2019时不起作用,但是当日期是01/03/2019和31/03/2019时可以正常工作

Is there any solution ? 有什么解决办法吗?

If at all possible, change the datatype from a varchar to a date, datetime or timestamp. 如果有可能,请将数据类型从varchar更改为日期,日期时间或时间戳。 The problem here is that the database doesn't understand how to order the date columns chronologically -- since it thinks it's dealing with standard text and wants to sort them alphabetically. 这里的问题是数据库不了解如何按时间顺序排列日期列,因为它认为它正在处理标准文本,并且希望按字母顺序对它们进行排序。 Using datetime allows it to sort the rows by date a select the subset you want. 使用datetime可以按日期对行进行排序,然后选择所需的子集。

Fortunately you can often just change the datatype of the column and as long as the values are in a standard format (And your table isn't millions and millions of records) it will usually convert them just fine. 幸运的是,您通常可以只更改列的数据类型,只要这些值采用标准格式(并且您的表不是数百万个记录),通常就可以很好地进行转换。 I'm assuming you're using mysql here, but most databases should have something similar. 我假设您在这里使用mysql,但是大多数数据库应该具有类似的内容。

ALTER TABLE <tableName> MODIFY <columnName> datetime;

Of course, I'd suggest making a copy of the table and trying out the conversion on that first -- just to be safe. 当然,为了安全起见,我建议您先复制表格并尝试进行转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将joomla的日期格式更改为d / m / Y? - how can i change date format for joomla to d/m/Y? 如何使用php以YMD格式获取批处理结束日期 - How can I get the batch end date in Y-M-D format using php 在whereBetween查询中将laravel中的created_at列转换为ymd格式 - convert created_at column in laravel to y-m-d format in whereBetween query 我应该如何将任何类型的日期格式从数据库转换为这种格式“d/m/Y”? - How should i convert any type of date format from database to this format "d/m/Y "? 如何在PHP中以DMY格式按Mysql数据库中的日期列显示并基于它进行搜索 - How Can I Display By Date Column From Mysql Database In D-M-Y Format in PHP And Make Search Based on it 如何使用php转换此时间格式-Y / m / d日期格式为1480550400000 + 0000 - How do I convert this time format - 1480550400000+0000 in Y/m/d date format using php 我如何将下面的代码修改为 output 格式为 dmY H:i:s 的日期? - how can i modify the code below to output a date in the the format d-m-Y H:i:s? 如何将j / m / y日期格式更改为时间戳? - How can i change j/M/y date format to timestamp? 在两(2)个日期之间,日期格式为列(Ymd) - Between two(2) Dates with date format for column (Y-m-d) PHP:将日期格式 d/m/Y h:i:sa 更改为 m/d/Y h:i:sa - PHP: Change Date format d/m/Y h:i:s a to m/d/Y h:i:s a
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM