简体   繁体   中英

Retrieving table according to the given date range

There is table called "table_name" which have 4 columns

id    | salary | emp_id | joining_date
---------------------------------------------
1     | 2900   | ep4354 | 2016-04-15 11:15:18
----------------------------------------------
2     | 3001   | ep7354 | 2016-04-16 07:13:01
----------------------------------------------
3     | 4201   | ep7365 | 2016-04-17 08:19:32
----------------------------------------------

Now I want to retrieve table from date 2016-04-16 to 2016-04-17 (ignore time). My query in laravel is-

$table = DB::select("SELECT * FROM table_name WHERE  date(joining_date)>= '".$from."' AND date(joining_date) <= '".$to."'"); 

Where $from and $to is variable and say there values are $from =2016-04-16 and $to =2016-04-17. So it should display table with two columns as-

id    | salary | emp_id | joining_date
----------------------------------------------
2     | 3001   | ep7354 | 2016-04-16 07:13:01
----------------------------------------------
3     | 4201   | ep7365 | 2016-04-17 08:19:32

Query is working fine, Thankful to Ankit Verma who helped me within few minutes just after putting my question. I just edited my question to those who said "Question is not clear" still after getting right answer.

Although I want more better query for the same if you guys have.

在Laravel中,使用以下代码:$ table = DB :: select(“ SELECT * FROM users WHERE date(created_at)='”。$ from。“'”);

您可以在mysql连接器中尝试

DATE(date) = $date

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM