mysql - 在 1 个查询中将 1 个表行中的多个 ID 查询到另一个表
问题描述
我在 MySQL
table1
ID|name|group_1|group_2|group_3
Table2
ID|group_name|Group_location中有 2 个表
我想获取表 1 ID 并显示所有数据而不是 tableID。我正在尝试使用这样的连接
SELECT table1.name, table2.group_1, table1.group_2, table1.group_3
FROM tabl1 JOIN table2 on table1.group_1 = table2.ID AND
table1.group_2 = table2.ID AND table1.group_3 = table2.ID
WHERE table1.ID IN (868)
ORDER BY FIELD(table1.ID,868);
解决方案
这是你想要的吗?
SELECT t1.name, t21.group_1, t22.group_2, t23.group_3
FROM table1 t1 LEFT JOIN
table2 t21
ON t1.group_1 = t21.ID LEFT JOIN
table2 t21
ON t1.group_2 = t22.ID LEFT JOIN
table2 t21
ON t1.group_3 = t23.ID
WHERE t1.ID IN (868)
ORDER BY FIELD(t1.ID, 868);
这LEFT JOIN
在任何参考列是 的情况下使用NULL
。
推荐阅读
- winapi - AnimateWindow() 在高 dpi 上错误地绘制背景 (Win10)
- elixir - 当 2 个库共享相同的依赖项并需要不同的版本时,混合 deps 错误
- asp.net-mvc - DropDownListFor 中的值未将值保存到数据库
- laravel - Laravel 4.0 到 5.7 迁移
- .htaccess - htaccess 规则在访问根域 URL 时在特定路径下显示我的网站
- flex-lexer - Flex 没有正确解析变量名
- javascript - 仅当定义了变量时,如何在 JavaScript 中运行代码?
- java - Spring - 自动装配与手动方法调用
- python - 是否可以从服务中使用相同的 gnome 终端窗口
- c++11 - 由于移动承诺导致的分段错误