Rajdipsinhji Hada
Rajdipsinhji Hada

Reputation: 97

How to get same date, start time and end time form database where date my have multiple times

image one

I want Query in PHP Mysql.

I want to get result like if session_Date is same then it shows like 2021-04-02 only one time and session_start time will be first time means 09:30 and last time means 12:30

that means session_date will display only one time and times will display start time and end time.

so I want below formate

session_date | session_start_time | session_end_time
2021-04-02 | 09:30 | 12:30
2021-04-03 | 09:30 | 10:30

thank you in advance.

Upvotes: 1

Views: 169

Answers (2)

Alexander Peresypkin
Alexander Peresypkin

Reputation: 61

You could try:

SELECT
 session_date,
 MIN(session_start_time),
 MAX(session_end_time)
FROM session
GROUP BY session_date

UPDATE:

For your case, you could do the following:

SELECT
  session_date,
  TIME_FORMAT(MIN(STR_TO_DATE(session_start_time, '%h:%i %p')), '%h:%i %p'),
  TIME_FORMAT(MAX(STR_TO_DATE(session_end_time, '%h:%i %p')), '%h:%i %p')
FROM session 
GROUP BY session_date;

But I would suggest you store date and time in fields with the respective types, that is session_date as DATE and session_start_time and session_end_time as TIME type.

Upvotes: 1

tamrat
tamrat

Reputation: 1940

You can try to group the values. If using eloquent and assuming your model is sessions:

Session::groupBy('session_date')->get();

Upvotes: 0

Related Questions