As above, I'm unsure on how to do it, although I am possibly overlooking something simple
I want to retrieve the timestamps and another column, just the most recent 7 which I have done using LIMIT and ordered them using ORDER BY timestamp DESC to get the most recent 7... But once retrieved, I'd like them oldest first rather than newest first
Anyone able to assist please?
Thanks!
two selects could work in this case. it would at least be one possible way to achieve what you want. i'm not sure if it would the best way.
i'm assuming your table has an id field.
select * from records
where id in(select id from records order by timestamp desc limit 7)
order by timestamp asc;
this lets you get the latest 7 rows in the inner select, then sort them in ascending order.
A nested query should take care of this. Something like
SELECT *
FROM (
SELECT timestamp, anotherColumn
FROM tableName
ORDER BY timestamp DESC
LIMIT 7 )
ORDER BY timestamp ASC;
Related
I'm trying to order my rows by the date that each row has.
One row has 2016-09-15 15:36 and one has 2016-08-15 13:12 How would i order them so that the highest one is at top?
Their row is called th_activity and i know that in a normal query, you would do something like SELECT * FROM threads ORDER BY th_activity DESC but i believe i know the problem. I use varchar as the type for the row. I'm not really sure what the length/value should be for date if that's what i must use.
If someone can explain how i would order this properly, i'd appreciate it.
If your date is year-month-day hour:minute, you can order alphabetically without any problems, so ORDER BY th_activity DESC should work fine.
You could have set the type of column as timstamp instead of varchar, which doesn't care about the length of data (only that while inserting data take care about the format).
Check this question to ordery by timestamp:
MYSQL - Order timestamp values ascending in order, from newest to oldest?
SELECT th_activity
FROM tableName
ORDER BY timestamp desc;
could be you need a str_to_date conversion
select * from threads
ORDER BY str_to_date(th_activity, '%Y-%m-%d %h:%i') DESC
I would use a data type
datetime
and use the same select you wrote in the question :
order by th_activity DESC
(question edited) My table has id,status,date,sequence
Situation: Get ids which satisfies:
date is max, not more than today's and status is A
if more than 1 status' with same date then get id only if it has max sequence
I am writing MYSQL in dbVisulizer.
edit: trying with query:
select id, max(date), max(sequence) from
table
where
table.date<=now() and status='A'
order by date desc,sequence desc
It sounds like I am just asking direct question without trying anything by myself, but for this situation I am completely stuck, I tried with case when but couldn't really accomplish any good, any starting point would be appriciated.
select id, max(date), max(sequence) from
table
where
table.date<=now() and status='A'
order by date desc,sequence desc
group_by id;
This should give you the desired results. Adapt table and field names to your table and fields.
select id from
table
where
table.date<now() and table.status='A'
order by date desc,sequence desc
limit 1;
You should check it for mistakes by yourself.
I have a table "A" with a "date" field. I want to make a select query and order the rows with previous dates in a descending order, and then, the rows with next dates in ascending order, all in the same query. Is it possible?
For example, table "A":
id date
---------------------
a march-20
b march-21
c march-22
d march-23
e march-24
I'd like to get, having as a starting date "march-22", this result:
id date
---------------------
c march-22
b march-21
a march-20
d march-23
e march-24
In one query, because I'm doing it with two of them and it's slow, because the only difference is the sorting, and the joins I have to do are a bit "heavy".
Thanks a lot.
You could use something like this -
SELECT *
FROM test
ORDER BY IF(
date <= '2012-03-22',
DATEDIFF('2000-01-01', date),
DATEDIFF(date, '2000-01-01')
);
Here is a link to a test on SQL Fiddle - http://sqlfiddle.com/#!2/31a3f/13
That's wrong, sorry :(
From documentation:
However, use of ORDER BY for individual SELECT statements implies nothing about the order in which the rows appear in the final result because UNION by default produces an unordered set of rows. Therefore, the use of ORDER BY in this context is typically in conjunction with LIMIT, so that it is used to determine the subset of the selected rows to retrieve for the SELECT, even though it does not necessarily affect the order of those rows in the final UNION result. If ORDER BY appears without LIMIT in a SELECT, it is optimized away because it will have no effect anyway.
This should do the trick. I'm not 100% sure about adding an order in a UNION...
SELECT * FROM A where date <= now() ORDER BY date DESC
UNION SELECT * FROM A where date > now() ORDER BY date ASC
I think the real question here is how to do the joining once. Create a temporary table with the result of joining, and make the 2 selects from that table. So it will be be time consuming only on creation (once) not on select query (twice).
CREATE TABLE tmp SELECT ... JOIN -- do the heavy duty here
With this you can make the two select statenets as you originally did.
I have a mysql db which one of the columns is of type timestamp. And the records look like this
How do I select the most recent timestamp in a query and display it?
SELECT * FROM tbl ORDER BY created_ts DESC LIMIT 1
PS: you should use hashed passwords rather than plain-text ones
It's simple:
SELECT * FROM TABLE ORDER BY CREATED_TS LIMIT 1
I guess its better to index the sequence ID and get the max from there. Getting max of your timestamp will run into real performance issues when your table grows big.
What's the most efficient way to select the last n number of rows in a table using mySQL? The table contains millions of rows, and at any given time I don't know how large the table is (it is constantly growing). The table does have a column that is automatically incremented and used as a unique identifier for each row.
SELECT * FROM table_name ORDER BY auto_incremented_id DESC LIMIT n
Actually the right way to get last n rows in order is to use a subquery:
(SELECT id, title, description FROM my_table ORDER BY id DESC LIMIT 5)
ORDER BY tbl.id ASC
As this way is the only I know that will return them in right order. The accepted answer is actually a solution for "Select first 5 rows from a set ordered by descending ID", but that is most probably what you need.
(Similar to "marco"s answer,)
my fav is the max()-function of MySQL too, in a simple one-liner, but there are other ways of sure:
SELECT whatever FROM mytable WHERE id > (SELECT max(id)-10 FROM mytable);
... and you get "last id minus 10", normally the last 10 entries of that table.
It's a short way, to avoid the a error 1111 ("Invalid use of group function") not only if there is a auto_increment-row (here id).
The max()-function can be used many ways.
Maybe order it by the unique id descending:
SELECT * FROM table ORDER BY id DESC LIMIT n
The only problem with this is that you might want to select in a different order, and this problem has made me have to select the last rows by counting the number of rows and then selecting them using LIMIT, but obviously that's probably not a good solution in your case.
Use ORDER BY to sort by the identifier column in DESC order, and use LIMIT to specify how many results you want.
You would probably also want to add a descending index (or whatever they're called in mysql) as well to make the select fast if it's something you're going to do often.
This is a lot faster when you have big tables because you don't have to order an entire table.
You just use id as a unique row identifier.
This is also more eficient when you have big amounts of data in some colum(s) as images for example (blobs). The order by in this case can be very time and data consuming.
select *
from TableName
where id > ((select max(id) from TableName)-(NumberOfRowsYouWant+1))
order by id desc|asc
The only problem is if you delete rows in the interval you want. In this case you would't get the real "NumberOfRowsYouWant".
You can also easily use this to select n rows for each page just by multiplying (NumberOfRowsYouWant+1) by page number when you need to show the table backwards in multiple web pages.
Here you can change table name and column name according your requirement . if you want to show last 10 row then put n=10,or n=20 ,or n=30 ...etc according your requirement.
select * from
(select * from employee
Order by emp_id desc limit n)
a Order by emp_id asc;