A
A
Alexander Vasyuchenko2014-12-01 10:34:47
MySQL
Alexander Vasyuchenko, 2014-12-01 10:34:47

SELECT query with initial id?

There is a table with fields: id INT, name VARCHAR(255), date DATE.
How can I form a SELECT query sorted by the date field so that records start with a fixed id value?
Let me explain.
the entire table is sorted by date, while the id can go as you like, for example like this: 10, 20, 3, 40, 15, 1, 17. I need to get a bunch of rows with a query (LIMIT 10 for example), but so that each pack starts with a specific id. For example, you need with id=40, then there will be a sequence: 40, 15, 1, 17, etc.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
A
Alexey Ivanov, 2014-12-01
@alexv1981

If all dates in the table are unique, then the following query will do
: select * from tbl
where `date` >= (select `date` from tbl where id = 40)
order by `date`;

A
Armenian Radio, 2014-12-01
@gbg

where will work just fine under these conditions:

select id,name,"DATE" from your_table order by "DATE",id,name where id>=N

M
Max, 2014-12-01
@AloneCoder

You can try to do it with a subquery that will select by date, and the main query will filter id
But something tells me that you initially have something wrong with the architecture

S
Sergey Romanov, 2014-12-01
@Serhioromano

Zprros
and
Absolutely identical in terms of the set of records that will be returned. Only the sorting of records will be changed. But records and there and there tezhe.
You need to understand that ORDER BY does not depend on WHERE. You can just do
AND again get the same records just not sorted.

F
faustxp, 2014-12-01
@faustxp

Then so.

select * from tbl b
where b.date>= (select a.date from (select date from tbl
where id = 40
order by date) a
limit 1)
order by b.date;

But only if the combination of ID and DATE is unique.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question