选择查询,但显示记录3的结果 [英] Select query but show the result from record number 3

查看:55
本文介绍了选择查询,但显示记录3的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些简单的查询:

SELECT * FROM table 

你们都知道结果:

|id|   foo   |    bar   |
-------------------------
|1 |   aaa   |    123   |
|2 |   bbb   |    234   |
|3 |   ccc   |    345   |
|4 |   ddd   |    456   |

但是,我想从记录3中显示什么?我知道我可以用SELECT * FROM table where id=3来做到这一点,但是我需要设置我选择的记录在第一位,可以说我选择id=3,所以结果如下:

but, what I want to do show from record number 3? I know I can do it with SELECT * FROM table where id=3, but I need to set the record I choose is in first rank, let say I choose id=3 so the result as follow:

|id|   foo   |    bar   |
-------------------------
|3 |   ccc   |    345   |
|4 |   ddd   |    456   |
|1 |   aaa   |    123   |
|2 |   bbb   |    234   |

|id|   foo   |    bar   |
-------------------------
|3 |   ccc   |    345   |
|1 |   aaa   |    123   |
|2 |   bbb   |    234   |
|4 |   ddd   |    456   |

这可能吗?

推荐答案

通过这种方式,您首先获得id = 3:

This way you get id = 3 first:

SELECT *
FROM tbl
ORDER BY (id = 3) DESC
-- ,id

如果要其余订单也按id订购.

Order by id additionally if you want the rest ordered, too.

该表达式的计算结果为布尔值 . FALSE(在mysql中= 0)在TRUE(在mysql中= 1)之前排序,因此我们对降序进行排序.

The expression evaluates to boolean. FALSE (= 0 in mysql) sorts before TRUE (= 1 in mysql), so we order descending.

它还将自动覆盖idNULL的情况.我在此处再次引用了手册:

It also automatically covers the case of id being NULL. I quote the manual again here:

在执行ORDER BY时,如果执行以下操作,则会首先显示NULL值 ORDER BY ... ASC,如果执行ORDER BY ... DESC,则最后一个.

When doing an ORDER BY, NULL values are presented first if you do ORDER BY ... ASC and last if you do ORDER BY ... DESC.

这篇关于选择查询,但显示记录3的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆