4

假设我有一个包含items的表和另一个包含subitems的表。我想返回与有限数量的项目相关的所有子项目。本质上,我想加入这两个查询:

SELECT * FROM subitem

SELECT * FROM item LIMIT 10

其中subitem.item = item.id

我试过这个:

SELECT * FROM subitem INNER JOIN item ON subitem.item = item.id LIMIT 10

但是,此查询仅返回 10 个子项(如您所料)。我想检索所有子项,同时仅将项目数限制为 10。我该如何实现?

4

4 回答 4

5

这将为您提供 10 件物品。但是,您应该添加 WHERE 子句和 ORDER BY 子句来获取您要查找的项目。

SELECT * FROM subitem INNER JOIN 
              (SELECT * FROM items LIMIT 10) AS I
              ON subitem.item = I.id 
于 2012-07-23T17:20:01.360 回答
3

尝试以下查询:

select * from item,subitem where id = item and 
  id in (select id from item limit 10)

IN如果in有问题LIMIT,请尝试以下查询:

select * from (select id from item limit 10) as i1, subitem where id =item ;
于 2012-07-23T17:20:48.237 回答
0

也许是这样的:

SELECT * FROM subitem WHERE item IN (SELECT id FROM item LIMIT 10);

于 2012-07-23T17:21:28.893 回答
-1

如何在此查询中使用限制

select tbl_thread.id,user_id,name,tbl_User.alert_flag,thread_name,thread_status,(select CAST(count(*) as UNSIGNED) from tbl_message where thread_id=tbl_thread.id AND read_status='0' and msg_from='EU' ) as unread,thread_timestamp,(select CAST(max(id) as UNSIGNED) from tbl_message where thread_id=tbl_thread.id) as max_id from tbl_thread,tbl_User where tbl_User.UserID=user_id ;
于 2018-04-26T11:30:38.037 回答