I have field(time) with data like this:
9:00-11:00 am
7:00-9:00 am
6:30-7:30 pm
1:00-2:30 pm
Select * from table order by time ASC
result:
1:00-2:30 pm
6:30-7:30 pm
7:00-9:00 am
9:00-11:00 am
I cant sort it correctly. Any help will do.
Thanks
Store your times in two separate TIME fields (start_time and end_time for example), which can reasonably be sorted by the database. Just storing a time range as text doesn't tell the database anything, it can't magically understand what it means.
Store your date ranges in two TIMESTAMP columns in MySQL, then you can sort on the columns however you want. You can even do more advanced sorts like sorting on the duration between the two timestamps easily using MySQL.
I am not recommending that you do this, but to show you why you'd want to split this up into multiple columns as #deceze recommended.
You can use mysql's STR_TO_DATE() to (kindof) convert the string to a date. Note that it completely ignores the first part of the time range, so it's effectively sorting on:
11:00 am
9:00 am
7:30 pm
2:30 pm
The query is:
SELECT
time,
STR_TO_DATE(time,'%h:%i-%h:%i %p')
FROM time_table
ORDER BY STR_TO_DATE(time,'%h:%i-%h:%i %p') ASC
A second query that effectively sorts on this:
9:00am
7:00am
6:30pm
1:00pm
is:
SELECT
time,
STR_TO_DATE(CONCAT(SUBSTRING_INDEX(time,'-',1), SUBSTRING(time,LOCATE(' ', time)+1)), '%h:%i%p')
FROM time_table
ORDER BY STR_TO_DATE(CONCAT(SUBSTRING_INDEX(time,'-',1), SUBSTRING(time,LOCATE(' ', time)+1)), '%h:%i%p') ASC
(I'm certain someone that's more proficient with regular expressions would be able to shorten this one).
Related
how to search between two date , when date format in database like :
2015-10-10 02:23:41 am
i just want to search between two date with format :
2015-10-10
without
02:23:41 am
any ideas please ?
Your question isn't completely clear. But, I guess you hope to find all the rows in your table where Date occurs on or after midnight on 2015-08-05 and before midnight on 2015-09-11 (the day after the end of the range you gave in your question.
Those two search criteria will find all the rows with Date values in the range you specified. (I'm ignoring the 02 at the end of 2015-09-10 02 in your question because I can't guess what it means, if anything.)
Try this query:
SELECT *
FROM table
WHERE `Date` >= '2015-08-05'
AND `Date` < '2015-09-10' + INTERVAL 1 DAY
This has the benefit that it can exploit an index on the Date column if you have one, so it can be fast.
You could write
SELECT *
FROM table /* slow! */
WHERE DATE(`Date`) BETWEEN '2015-08-05' AND '2015-09-10'
That's slightly easier to read, but the WHERE condition isn't sargable, so the query will be slower.
Notice that the beginning of the range uses >= -- on or after midnight, and the end of the range uses < -- before midnight.
Pro tip: Avoid the use of reserved words like DATE for column names. If you make mistakes writing your queries, their presence can really confuse MySQL, not to mention you, and slow you down.
May I suggest:
select * from table where cast(date as date) between '2015-08-05' and '2015-09-10'
When your where clause is based on a timestamp, but you're using date as the parameters for your between, it excludes anything that happens on the second date unless it happened precisely at midnight.
When using the end date for the range, include the time of the end of the day:
SELECT *
FROM YourTable
WHERE date BETWEEN '2015-08-05' AND '2015-09-10 23:59:59'
Here's my sql query :
SELECT uid, title, startTime, endTime, cover FROM event ORDER BY startTime DESC
But this will bring me elements in this order :
12/30/2013 00:00
12/25/2013 00:00
01/10/2014 00:00
Looks like it's sorting only using days and months.
Do you have any idea about this ?
Thank you.
In case you really have stored them as string, STR_TO_DATE() should be able to solve this.
SELECT uid, title, startTime, endTime, cover
FROM event
ORDER BY STR_TO_DATE( startTime, '%m/%d/%Y' ) DESC
Your dates are being stored as a string or varchar, which means the standard formatting or DATETIME format is thrown completely out the window.
If you re-format the column to a DATETIME, you would get rows like this
2013-12-15 00:00:00
2013-12-30 00:00:00
2014-01-10 00:00:00
You can see that the most significant unit is the first in the string order, whereas the smallest unit is the last in the string, thus allowing for simple ordering.
Using #Lars suggestion to have the STR_TO_DATE() function with ordering, you should run the following query after creating a new column (let's call it newStartTime) of the structure DATETIME:
UPDATE event SET newStartTime = STR_TO_DATE( startTime, '%m/%d/%Y %h:%i' );
Then eventually throw out the startTime column after you depreciate its usage in the application that you've taken over. You should do this because string to DATETIME conversion is time-consuming and will result in long query execution times as you approach large result sets.
I have two field in a database table departureDateTime and arrivalDateTime and the values like
departureDateTime=03/18/2012 1:05 PM
arrivalDateTime=03/18/2012 3:15 PM
I have hundreds of records in the table.
I need to sort and display according to the duration from these two time. I know to calculate the duration from two dates
Duration=(strtotime($arrivalDateTime') - strtotime($departureDateTime))/3600
But how I write a mysql query to sort and display these 100 records from database
Does any one any idea?
Thanks
Change your departureDateTime to DATETIME instead of varchar or whatever you are currently using. Then you would use something like this:
SELECT other, stuff, TIMEDIFF(departureDateTime, arrivalDateTime) as theDifference FROM myTable ORDER BY theDifference ASC LIMIT 0, 100
If you kept your date and times in a date time column type then you wouldn't have this problem. Consider changing.
There are mysql functions to calculate dates and time too
http://dev.mysql.com/doc/refman/5.6/en/datetime.html
If not, you can do sorting in PHP using usort.
I have a SQLite database that has a time field set as a text type. And it contains values like 11:30 PM, 2:30 PM, etc and I need to select time and date by date & time ASC. I use the following query SELECT * FROM schedule ORDER BY date ASC, time ASC LIMIT 50
But the problem is I get values such as 11:30PM before 2:30PM
How can I avoid this?
Thanks!
the problem is that you are storing in human readable format a value that should be meaningful for a machine. Since sqlite doesn't have a native time type, you'll have to make do with the next nearest approximation, which is a numeric type. You could store both date and time as a single number, for instance as seconds since January 1st, 1970, and then format those values for presentation to users at the last moment.
How do you sort data which was stored in a mysql database depending on the days of the week in which the data was submited ??
I basically want to create a diary which outputs information in each day of the week depending on what day it was posted by dates so,
Mon - Data in order of date
Tue -
Wed - e.t.c
Any code examples and information will be great, thanks.
You can do a
SELECT DAYOFWEEK(datehere) as dayofweek, datehere FROM something ORDER BY dayofweek, datehere;
You can use the DAYOFWEEK function to extract the day, and then sort on it just like any other data.
What kinf of data type is the column where you store the date submission?
It seems like you're asking for a basic SELECT statement?
SELECT some_column, another_colum FROM your_table ORDER BY your_date_column DESC
This assumes you actually have a column that logs the insertion timestamp.
If this answer is obnoxiously simplistic, please forgive me...and give us more details :)
Regards.
If your data is stored as a DATE or DATETIME field, use the DAYOFWEEK or DATE_FORMAT functions to turn it into day name for output, but continue to order by the DATE field
SELECT DATE_FORMAT(my_date_column, '%W') AS dayofweek
FROM my_table
ORDER BY my_date_column
Well, the sorting bit is easy, just sort on the column that represents the post's date. The grouping in days is something you can do in your code, since you need to check the date there anyway (for post-processing the actual output).
To put it this way, you can do a subselect to get the specific day of the week, but in your code you would have to check the day again to group posts per day. In that case it's better (and cleaner, since you're separating business logic from data) to do this in your code, something like this:
select all posts (within date range)
make an associative array, with the
days as keys, and posts (in new
arrays) as values
loop through the
days and then posts to output the
posts per day
SELECT *
FROM diary_entries
ORDER BY FIELD(DAYOFWEEK(created), '2, 3, 4, 5, 6, 7, 1'), created
DAYOFWEEK grabs day of the week number (1 = Sunday).
FIELD makes Monday first day of the week.
After sorting by day of week, then sorted by date created.