我有 2 个关系:表和表组。一张表可以在一个表组中,一个表组包含多个表。
我需要一个返回以下输出的选择:
表组中 sortid 为 0 的所有表 -> 表再次按其 sortid 排序
所有表格依此类推。
这在 Sql(Mysql dbms) 中可能吗?
表格
table_id, name, pax, createdate, lastupdate, tablegroup_id, sort_id,
'2', 'tisch 02', '6', NULL, NULL, '1', '3'
'3', 'tisch 03', '4', NULL, NULL, '1', '1'
'4', 'tisch 04', '2', NULL, NULL, '1', '2'
'5', 'tisch 05', '8', NULL, NULL, '1', '4'
'6', 'tisch 101', '4', NULL, NULL, '2', '1'
'7', 'tisch 102', '6', NULL, NULL, '2', '2'
'8', 'stube 01', '2', NULL, NULL, '3', '2'
'9', 'stube 02', '3', NULL, NULL, '3', '1'
表格组
tablegroup_id, name, notiz, color, customer_id, sort_id, visible
'1', 'garten', NULL, '1', '1', '2', '1'
'2', 'lounge', NULL, '2', '1', '3', '1'
'3', 'stube', '', '7', '1', '1', '1'
预期输出:
'stube 01'
'stube 02'
'tisch 03'
'tisch 04'
'tisch 05'
'tisch 101'
'tisch 102'
最佳答案
内连接table
和 tablegroups
在一个共同的tablegroup_id
和 ORDER BY
sort_id
的 tablegroups
然后是 sort_id
的 table
.
SELECT t.name
FROM table t
INNER JOIN tablegroups tg
ON tg.tablegroup_id = t.tablegroup_id
ORDER BY tg.sort_id,
t.sort_id;
关于mysql - 两张表的sql排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51723565/