[英]How to select rows from another table if not enough rows in the first? With SQL
[英]select rows from SQL table and 5 rows from another
我有三个这样的SQL Server表
我需要categories from LS_categoires where nodeid = 183
选择categories from LS_categoires where nodeid = 183
并select only 5 files from LS_files
与每个类别相关的select only 5 files from LS_files
中select only 5 files from LS_files
如果我有两个与node 183
有关的类别,结果应该是10 rows
吗?
尝试这个:
SELECT
*
FROM LS_Categories c
INNER JOIN
(
SELECT
*, ROW_NUMBER() OVER(PARTITION BY catid
ORDER BY item_id ASC) rownum
FROM LS_ItemTypes
) l ON c.catid = l.catid
AND l.rownum <= 5
INNER JOIN LS_Files f ON l.item_id = f.id
where c.nodeid = 183;
这将为每个类别选择第一个文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.