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
Related
I would like my select statement only to print out the highest score of every submission from one email.
Table : scorelist
name
score
email
createdAT
I have try following query:
SELECT DISTINCT name, score FROM scorelist ORDER BY score DESC, createdAt ASC
This is what I've tried. I think I need a WHERE clause where I ask that email=email = limit 1 but it doesn't work as intended.
The query prints out every instance but not the duplicated scores so if a user have had the same score it's been removed by using DISTINCT of course.
If you want highest score according email, Then use group by clause with MAX(). try below query:
SELECT name, MAX(score) as score FROM scorelist
GROUP BY email ORDER BY score DESC, createdAt ASC;
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;
I want to select last 50 rows from MySQL database within column named id which is primary key. Goal is that the rows should be sorted by id in ASC order, that’s why this query isn’t working
SELECT
*
FROM
`table`
ORDER BY id DESC
LIMIT 50;
Also it’s remarkable that rows could be manipulated (deleted) and that’s why following query isn’t working either
SELECT
*
FROM
`table`
WHERE
id > ((SELECT
MAX(id)
FROM
chat) - 50)
ORDER BY id ASC;
Question: How is it possible to retrieve last N rows from MySQL database that can be manipulated and be in ASC order ?
You can do it with a sub-query:
SELECT * FROM
(
SELECT * FROM table ORDER BY id DESC LIMIT 50
) AS sub
ORDER BY id ASC;
This will select the last 50 rows from table, and then order them in ascending order.
SELECT * FROM table ORDER BY id DESC LIMIT 50
save resources make one query, there is no need to make nested queries
SELECT * FROM table ORDER BY id DESC, datechat DESC LIMIT 50
If you have a date field that is storing the date (and time) on which the chat was sent or any field that is filled with incrementally (order by DESC) or de-incrementally (order by ASC) data per row put it as second column on which the data should be ordered.
That's what worked for me!!!! Hope it will help!!!!
Use it to retrieve last n rows from mysql
Select * from tbl order by id desc limit 10;
use limit according to N value.
if anyone need this
you can change this into
SELECT
*
FROM
`table`
WHERE
id > ((SELECT
MAX(id)
FROM
chat) - 50)
ORDER BY id ASC;
into
SELECT
*
FROM
`table`
WHERE
id > (SELECT MAX(id)- 50 FROM chat)
ORDER BY id ASC;
select * from Table ORDER BY id LIMIT 30
Notes:
* id should be unique.
* You can control the numbers of rows returned by replacing the 30 in the query
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;
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.