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.
Related
I have a table in my database which is updated randomly. I'm trying to pull entries by the latest date. This part is simply and I can do it with ease. However, I want to pull the two latest dates.
Example; If my last update was 2015-06-22 and the one before than was 2015-06-12 and the one before then was 2015-06-02. I would want to pull 2015-06-22 and 2015-06-15.
I would use a LIMIT 2, however, there are an unknown amount of items that may have the same date attached.
I haven't tried anything other than the LIMIT 2. After some research, I wasn't able to find anything to reference.
Update
I used SELECT DISTINCT to get the desired results.
SELECT DISTINCT dates FROM table ORDER BY dates DESC LIMIT 2
Will give you the latest 2 dates in the table.
I would have a column set to id, that is auto incremented, and do my query like this:
SELECT * FROM tbl_name ORDER BY `id` DESC LIMIT 2
Crap McAdam you beat me to it!
You can get the latest two dates using LIMIT, like you mentioned:
SELECT latestDates
FROM myTable
ORDER BY dateColumn DESC
LIMIT 2;
And you can join that to your original table to only select rows that occur on those two dates:
SELECT m.*
FROM myTable m
JOIN(
SELECT latestDates
FROM myTable
ORDER BY dateColumn DESC
LIMIT 2) tmp ON tmp.latestDates = m.dateColumn;
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;
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;
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
So i have a whole load of votes going into a voting system. I want to display how many votes i have in any one day. But i also want to then, display the amount of votes per day and spit out which day they were voted on, i.e 24k votes on 05/06/12, 27k votes on 06/06/12
SELECT count(*) AS count
FROM results
GROUP BY DAY(datesubmitted), YEAR(datesubmitted), MONTH(datesubmitted)
ORDER BY DAY(datesubmitted) DESC, YEAR(datesubmitted) DESC, MONTH(datesubmitted) DESC
Is my query, i tried to add something like
DAY(FROM_UNIXTIME(datesubmitted)) as order_day
but this just throws a null which i found interesting as i'd expect the query to fail as there aren't any outers.
Why don't you simply GROUP BY datesubmitted DESC? Also, no need to ORDER BY if it's following the same criteria as GROUP BY.
Number of votes on a specific day:
SELECT COUNT(*) AS total
FROM results
WHERE datesubmitted BETWEEN #dateMin AND #dateMax
Number of votes for each separate day:
SELECT COUNT(*) AS total, DATE(datesubmitted) AS day
FROM results
GROUP BY DATE(datesubmitted)
ORDER BY DATE(datesubmitted) DESC
UPDATED AGAIN
So just saw a new answer but for me how i got it working was:
SELECT count(*) AS count, DAY(datesubmitted) AS newday,
YEAR(datesubmitted) as newyear ,MONTH(datesubmitted) as newmonth
FROM results
GROUP BY DAY(datesubmitted), YEAR(datesubmitted), MONTH(datesubmitted)
ORDER BY YEAR(datesubmitted) DESC, MONTH(datesubmitted) DESC, DAY(datesubmitted) DESC
This way i get the correct ordering with year, month and day properly and also it displays the dates. I could concat them, but thats for another day.