繁体   English   中英

从SQL表中选择行,从另一个表中选择5行

[英]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 = 183select only 5 files from LS_files与每个类别相关的select only 5 files from LS_filesselect 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM