在为摄影项目连接 mysql 数据库中的两个表时需要帮助
Need help in joining two tables from mysql database for a photography project
我正在从事摄影项目,在加入 table 和从 mysql 数据库检索数据时遇到了一些问题。
我为这个项目创建了两个 table。一个名为 cm_team 的 table 用于团队成员,另一个名为 cm_events 的 table 用于photography events..假设要拍摄一个事件,我们需要6个人,并且这个人的id存储在cm_eventstable中。
如上图所示。我将 cm_team 成员的 ID 存储在 cm_events table 中。我希望获得团队名称cm_events table 中相应突出显示字段中的成员。非常感谢任何帮助。
例如我想要的输出应该是:而不是 team_lead 标题下的 5,我应该得到对应于 5 的名字,即 Arjun
是这样的吗? (比子查询更干净、更快)
SELECT
`event`.client_name,
`event`.client_number,
# some more event cols ..
`team_lead`.`cm_name` AS `team_lead`,
`candid_photo`.`cm_name` AS `candid_photo`,
`candid_video`.`cm_name` AS `candid_video`,
`traditional_photo`.`cm_name` AS `traditional_photo`,
`traditional_video`.`cm_name` AS `traditional_video`,
`helper`.`cm_name` AS `helper`
FROM cm_events `event`
JOIN cm_team `team_lead` ON `team_lead`.`cm_code` = `event`.`team_lead`
JOIN cm_team `candid_photo` ON `candid_photo`.`cm_code` = `event`.`candid_photo`
JOIN cm_team `candid_video` ON `candid_video`.`cm_code` = `event`.`candid_video`
JOIN cm_team `traditional_photo` ON `traditional_photo`.`cm_code` = `event`.`traditional_photo`
JOIN cm_team `traditional_video` ON `traditional_video`.`cm_code` = `event`.`traditional_video`
JOIN cm_team `helper` ON `helper`.`cm_code` = `event`.`helper`
有子查询
DROP TABLE IF EXISTS T,t1;
CREATE TABLE T (
id int, name varchar(10));
insert into t values
(1 , 'aaa'),
(2 , 'bbb');
create table t1 (
id int, team_lead int,team_a int);
insert into t1 values
(1,1,2),
(2,2,2);
select t1.id, (select t.name from t where t.id = t1.team_lead) team_lead,
(select t.name from t where t.id = t1.team_a) team_a
from t1;
+------+-----------+--------+
| id | team_lead | team_a |
+------+-----------+--------+
| 1 | aaa | bbb |
| 2 | bbb | bbb |
+------+-----------+--------+
2 rows in set (0.001 sec)
我正在从事摄影项目,在加入 table 和从 mysql 数据库检索数据时遇到了一些问题。
我为这个项目创建了两个 table。一个名为 cm_team 的 table 用于团队成员,另一个名为 cm_events 的 table 用于photography events..假设要拍摄一个事件,我们需要6个人,并且这个人的id存储在cm_eventstable中。
如上图所示。我将 cm_team 成员的 ID 存储在 cm_events table 中。我希望获得团队名称cm_events table 中相应突出显示字段中的成员。非常感谢任何帮助。
例如我想要的输出应该是:而不是 team_lead 标题下的 5,我应该得到对应于 5 的名字,即 Arjun
是这样的吗? (比子查询更干净、更快)
SELECT
`event`.client_name,
`event`.client_number,
# some more event cols ..
`team_lead`.`cm_name` AS `team_lead`,
`candid_photo`.`cm_name` AS `candid_photo`,
`candid_video`.`cm_name` AS `candid_video`,
`traditional_photo`.`cm_name` AS `traditional_photo`,
`traditional_video`.`cm_name` AS `traditional_video`,
`helper`.`cm_name` AS `helper`
FROM cm_events `event`
JOIN cm_team `team_lead` ON `team_lead`.`cm_code` = `event`.`team_lead`
JOIN cm_team `candid_photo` ON `candid_photo`.`cm_code` = `event`.`candid_photo`
JOIN cm_team `candid_video` ON `candid_video`.`cm_code` = `event`.`candid_video`
JOIN cm_team `traditional_photo` ON `traditional_photo`.`cm_code` = `event`.`traditional_photo`
JOIN cm_team `traditional_video` ON `traditional_video`.`cm_code` = `event`.`traditional_video`
JOIN cm_team `helper` ON `helper`.`cm_code` = `event`.`helper`
有子查询
DROP TABLE IF EXISTS T,t1;
CREATE TABLE T (
id int, name varchar(10));
insert into t values
(1 , 'aaa'),
(2 , 'bbb');
create table t1 (
id int, team_lead int,team_a int);
insert into t1 values
(1,1,2),
(2,2,2);
select t1.id, (select t.name from t where t.id = t1.team_lead) team_lead,
(select t.name from t where t.id = t1.team_a) team_a
from t1;
+------+-----------+--------+
| id | team_lead | team_a |
+------+-----------+--------+
| 1 | aaa | bbb |
| 2 | bbb | bbb |
+------+-----------+--------+
2 rows in set (0.001 sec)