将两个sql select查询(在postgres中)与LIMIT语句组合 [英] Combine two sql select queries (in postgres) with LIMIT statement

查看:118
本文介绍了将两个sql select查询(在postgres中)与LIMIT语句组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,我想要一个查询,该查询返回创建的最后10条记录以及ID为x的记录。

I've got a table and I want a query that returns the last 10 records created plus the record who's id is x.

我正在尝试执行-

SELECT * FROM catalog_productimage
ORDER BY date_modified
LIMIT 10
UNION
SELECT * FROM catalog_productimage
WHERE id=5;

但是看起来我不能放 LIMIT UNION 之前的位置。我尝试添加另一列并将其用于排序-

But it doesn't look like I can put LIMIT in there before UNION. I've tried adding another column and using it for sorting -

SELECT id, date_modified, IF(false, 1, 0) as priority FROM catalog_productimage
UNION
SELECT, id, date_modified, IF(true, 1, 0) as priority FROM catalog_productimage
WHERE id=5
ORDER BY priority, date_modified
LIMIT 10;

但我的进步不大。

推荐答案

只需检查一下是否可行:

Just checked that this will work:

(SELECT * FROM catalog_productimage
ORDER BY date_modified
LIMIT 10)
UNION
SELECT * FROM catalog_productimage
WHERE id=5;

这篇关于将两个sql select查询(在postgres中)与LIMIT语句组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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