如何以相反的顺序选择行(mysql)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9582393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I select rows in reverse order (mysql)
提问by Yura Borisenko
How can I select rows in reverse order (DB MySQL)?
如何以相反的顺序选择行(DB MySQL)?
For example,
I have a table with 12 rows (fields: id,location), I want select -4th row before a row with id = 6,
i.e. wanted row will have id = 'not necessarily 2',
but there is condition - where table.location='some_location'.
What should the content of request to mysql be like?
对mysql的请求内容应该是怎样的?
Editted at 30 minut later.
Here is solution!
Some example, I checked drodil's suggestion so:
30 分钟后编辑。
这里是解决方案!一些例子,我检查了 drodil 的建议,所以:
mysql> select * from subscrs where id < 100000 order by id desc limit 4;
+-------+--------+-----------+-------+
| uid | subscr | event | id |
+-------+--------+-----------+-------+
| 5307 | 5123 | feed_news | 99999 |
| 25985 | 5211 | feed_news | 99998 |
| 15123 | 130 | feed_news | 99997 |
| 28368 | 19497 | feed_news | 99996 |
+-------+--------+-----------+-------+
4 rows in set (0.00 sec)
Drodil, thank you!
卓尔,谢谢!
回答by drodil
Or you could do it without caring the deleted results, just get the fourth before the given id (6) by:
或者,您可以在不关心已删除结果的情况下执行此操作,只需通过以下方式获取给定 id (6) 之前的第四个:
SELECT * FROM SomeTable WHERE id < 6 ORDER BY id DESC LIMIT 4,1
回答by maxx233
The 4th row before a given row, or the 4th ID before a given ID? There may be IDs missing due to deleted records (ie.. IDs 101, 102, 104, 105, 111, ...)... If you just want an ID 4 less than some other ID for whatever reason (I would agree more info should be given here, as there's likely a better solution!) then you can simply do
给定行之前的第 4 行,还是给定 ID 之前的第 4 个 ID?由于删除的记录,可能会丢失 ID(即 ID 101、102、104、105、111,...)...应该在此处提供更多信息,因为可能有更好的解决方案!)然后你可以简单地做
SELECT * FROM SomeTable WHERE ID=6-4
You can expand on that if you want the ID 4 less than the ID of the row containing a specific location with
如果您希望 ID 4 小于包含特定位置的行的 ID,则可以对此进行扩展
SELECT * FROM SomeTable WHERE ID = (SELECT ID FROM SomeTable WHERE Location='Germany')-4
But again, please share some sample data and what you're trying to achieve/why you're planning to do it this way - this isn't a logical solution. If ID 2 had been deleted, or if your Location field does not contain unique data - then this will break. There must be a better design for what you're trying to do, but you need to provide more details for that.
但同样,请分享一些示例数据以及您想要实现的目标/为什么您打算这样做 - 这不是一个合乎逻辑的解决方案。如果 ID 2 已被删除,或者您的位置字段不包含唯一数据 - 那么这将中断。必须为您尝试做的事情提供更好的设计,但您需要为此提供更多详细信息。
回答by Aprillion
if my mindreading is still working, you probably need something like this:
如果我的读心术仍然有效,你可能需要这样的东西:
select t1.location, t2.location
from mytable t1
join mytable t2 on t2.id = t1.id - 4
where t1.location = 'some_location'
回答by Ian Leaf Fraud
Please add ORDER BY id DESC
in last of your query to fetch the data from last to first.
请添加ORDER BY id DESC
您的查询的最后一个以从最后到第一个获取数据。
OR
或者
For assenting order you can simply add ORDER BY id ASC
.
对于同意命令,您只需添加ORDER BY id ASC
.