I have a list of events in events_table, i store date of event in yyyy-mm-dd format.
I need to sort events by date:
(SELECT * from events_table ORDER BY date DESC)
But if I have more then 1 events on similar date, sorting works wrong.
I want to sort by date and by id, to view correct order(new events first).
I know that good practice is to use a timestamp, but since users can introduce information about event that was a day or two ago and this method not working in my case.
If you want first descending order by date and then ascending order by id then use below-
SELECT * from events_table ORDER BY date DESC, id ASC;
If you want first descending order by date and then descending order by id then use below-
SELECT * from events_table ORDER BY date DESC, id Desc;
If if id is primary key and auto_increment then there is no need to use ordering on date as ordering on id will be enough and will be more optimized.
SELECT * from events_table ORDER BY id Desc;
You can ORDER BY multiple fields.
SELECT * from events_table ORDER BY date DESC, id;
Related
I have a table in a mySQL database to which I want to sort the data in this way: the last 10 values of the temperature and time sorted by time order from oldest to newest.
The field in the table is called timeStamp and is of type TIMESTAMP
like this 2016-02-10 22:41:23
Doing this query
SELECT * FROM `tempLog` ORDER BY` timeStamp` DESC LIMIT 0, 10
I get the records in chronological order from most recent to oldest, but I want from oldest to newest and then put everything in a line graph
To get the last ten records sorted that way you could do this:
SELECT *
FROM (SELECT * FROM `tempLog` ORDER BY `time_stamp` DESC LIMIT 10) AS `foo`
ORDER BY `time_stamp` ASC;
In my order history, I am currently listing all the customers previous orders, however, not in date order.
My current query is :
SELECT OrderItemID, Date
FROM orderitems
WHERE OrderID=$orderid
GROUP BY OrderID;
After this query, I use a while loop, for each order found, to print the past order in a table.
However I want to print past orders by 'ORDER BY Date'. After the GROUPBY I put ORDER BY Date Desc, but had no difference in results printing.
Any guidance would be appreciated.
Since you are filtering the records with OrderID, why do you need to GROUP BY it?
SELECT OrderItemID, Date
FROM orderitems
WHERE OrderID=$orderid
ORDER BY Date DESC
I'm not sure why OrderItems would have the date of the order. Based on the text, I suspect you want something like this:
SELECT OrderID, Date
FROM Orders o
WHERE CustomerId = $CustomerId
ORDER BY Date DESC;
This is my PHP SQL query:
SELECT * FROM Buchungen
WHERE Buchungen.konto = '".$value_entry."'
ORDER BY datum DESC
Is it possible to order this by the newest id for each date? When i have 3 entries, from 18.11.2013, i want the newest entry on top. The latest entry has the largest id from this date. I don't want to add an time column in my sql table.
Replace:
ORDER BY datum DESC
With:
ORDER BY datum DESC, id DESC
Basically, this means: Order by datum desc but whenever there are duplicated values for datum then order those values by id desc.
You could just add a timestamp field to your table, and sort by that.
Or you could have an auto_increment index (can't see your schema, so I can't judge if you have it)
SELECT * FROM Buchungen WHERE Buchungen.konto = '".$value_entry."'ORDER BY datum DESC, id DESC
Ps, I hope $value_entry is cleaned/escaped user input if it is user input
I am working on an application for an organization.
Here i have some events in database table. Now client requirements for their view are as follow.
First event should be today's event (If there is any)
Then upcoming events should be from todays date. Like if today
01-11-2011 then next event should be of 02-11-2011(if there is any)
and next one should be of 03-11-2011 and so on....
Once all upcoming events have been listed, you can display old
events.
I am just wondering if i can accomplish this task in just one Query. Can you suggest me some query?
Here is table structure.
Currently i have this query.
SELECT * FROM tbl_event ORDER BY event_date DESC;
This application is in Codeignitor framework and MySql is database type.
SELECT ...
...
ORDER BY (event_date = curdate()) DESC, // today's events
(event_date > curdate()) DESC, // future events
event_date DESC; // past events
Something like this maybe?
SELECT * FROM tbl_event ORDER BY event_date DESC WHERE DATE(event_date) = DATE(NOW()) UNION SELECT * FROM tbl_event ORDER BY event_date DESC WHERE event_date >= NOW() UNION SELECT * FROM tlb_event ORDER_BY event_date DESC WHERE event_date < NOW()
I have a query something like this:
SELECT
title, desc, date
FROM
tablename
ORDER BY
date ASC, title ASC;
Works fine when the data actually has a date. Issue is, date submission is optional, so I sometimes get 0000-00-00 as a date, which has the unfortunate effect of placing all nondated rows on top.
So, I then tried this:
SELECT
title, desc, date
FROM
tablename
ORDER BY
date DESC, title ASC;
Which sort of works, but not really -- all items with dates (non 0000-00-00) get listed in descending order, followed by all items with 0000-00-00.
What I want to do is order by date ASC, title ASC, but only if the date != 0000-00-00, but if date is = 0000-00-00, then just ORDER BY title ASC on those (I think I explained that correctly).
The only ways I can think to do this are non-SQL based (either 2 queries, or, each query just populates an in-memory array, and then I sort using PHP).
Is there a SQL query that can do this?
ORDER BY date = '0000-00-00' ASC, date ASC, title ASC
Your 2 query solution is a good one, you can do it all in SQL using the UNION command.
The first query will be for dates that are non-zero, then UNION in the query for dates that are zero.
Edit: Something like:
SELECT * FROM tbl
WHERE DATE != '0000-00-00'
ORDER BY date ASC
UNION SELECT * FROM tbl
WHERE DATE = '0000-00-00'
ORDER BY title ASC
This may not be very useful in this instance, but for complex queries, UNION can come in handy.