我有两张桌子:
我正试图以这种方式对结果进行排序:
1 Section1
1 Item1 1
2 Item2 1
3 Item3 1
2 Section2
4 Item4 2
5 Item5 2
换句话说 - 放置属于部分本身下方部分的项目。
只使用一个查询就能实现这样的结果吗?
修改
现在我正在使用INNER JOIN
,但它并不完全适合我的目的:我需要知道查询中不同部分的确切数量,如果我能够确切地知道确切的数量会很好下一部分的位置(现在我只是按section_id
排序并寻找它的变化)
答案 0 :(得分:3)
或许这样,here is some fiddle
select
s._id sid,
s.name,
null iid
from
section s
union all
select
i.section_id sid,
i.name,
i._id iid
from
item i
order by
sid, iid
答案 1 :(得分:2)
可以像
那样完成select section._id as sectionId,section.name, 0 as itemId
from section
union
select item.section_id as sectionId, item.name, item._id as itemId
from item
order by sectionId, itemId