开发者

Selecting 5 rows from a table, but one row is specifically specified

This seems possible, but at the same time I am wondering if it actually is possible.

I have a table {users}. I want to pull 5 rows from that table, but at the same time, I want to specify one row.

The closest sql statement I get is:

SELECT 
    u.id, u.full_name, u2.id, u2.full_name
FROM 
    users u
JOIN 
    users u2 ON u2.id != 872
WHERE 
    u.id = 872
LIMIT 4

I know I can just do th开发者_开发百科is in two sql statements, but I want to try and only do it in one.

Thank you

Jason246


SELECT  *
FROM    users
ORDER BY
        u.id = 872 DESC, RANDOM()
LIMIT 5

More efficiently, though, will be to make it in a UNION:

SELECT  *
FROM    users
WHERE   id = 872
UNION ALL
SELECT  *
FROM    (
        SELECT  *
        FROM    user
        WHERE   id <> 872
        LIMIT 4
        ) q
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜