mysql - 两张表的sql排序

标签 mysql sql join

我有 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'

最佳答案

内连接tabletablegroups在一个共同的tablegroup_idORDER BY sort_idtablegroups然后是 sort_idtable .

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/

相关文章:

php - 仅当从程序内启动 xampp 服务器时,才返回问号而不是阿拉伯字符

mysql - SQL (mysql) - 如果给定列上的给定行作为特定值,则不列出该列

MySQL + Code First + Lazy Load 问题!

mysql - 我的查询需要哪种连接类型

mysql - 如何获得非重复项?

php - 当我向其中添加 LIMIT 关键字时,SQL 不工作

mysql - mysql 搜索域,没有 TLD 扩展名

mysql - Laravel,使用 JOIN 加载关系

MySQL 之间通过使用替换

MySQL 查询联盟脚本表