MySQL/多表查询:修订间差异
来自康健生活
< MySQL
| 第3行: | 第3行: | ||
<syntaxhighlight lang="mysql"> | <syntaxhighlight lang="mysql"> | ||
SELECT * | |||
FROM `table1` | |||
INNER JOIN `table2` ON `table1`.xxx_id = `table2`.id;</syntaxhighlight> | |||
== 联查 == | |||
<syntaxhighlight lang="mysql"> | |||
SELECT | |||
adh.id, | |||
adh.name, | |||
adh.team_id, | |||
adh.work_id, | |||
adh.group_id, | |||
adh.history_id, | |||
adh.resource, | |||
adh.format, | |||
adh.x, | |||
adh.y, | |||
adh.width, | |||
adh.height, | |||
t.name AS t_name, | |||
w.name AS w_name, | |||
u.name AS u_name, | |||
indah.name AS history_history_name, | |||
indah.id AS history_history_id, | |||
indah.create_time AS history_create_time, | |||
indah.version AS history_version, | |||
indah.message AS history_message, | |||
dah.create_time, | |||
dah.version, | |||
dah.message | |||
FROM | |||
`md_design_artboard` adh | |||
LEFT JOIN `md_design_artboard_history` `dah` ON `adh`.`history_id` = `dah`.`id` | |||
LEFT JOIN `md_design_artboard_history` `indah` | |||
ON `adh`.`id` = `indah`.`artboard_id` | |||
LEFT JOIN `md_user` `u` | |||
ON dah.user_id = `u`.`id` | |||
LEFT JOIN `md_team` `t` | |||
ON `adh`.`team_id` = `t`.`id` | |||
LEFT JOIN `md_work` `w` | |||
ON `adh`.`work_id` = `w`.`id` | |||
WHERE | |||
adh.team_id = '13' | |||
AND adh.work_id = '131' | |||
ORDER BY adh.name; | |||
</syntaxhighlight> | |||
=== 统计部门下属项目 === | |||
<syntaxhighlight lang="mysql"> | |||
SELECT | |||
`mw`.`id` AS `id`, | |||
`mw`.`name` AS `name`, | |||
`mu`.`department` AS `department`, | |||
`mtu`.`role_id` AS `role_id`, | |||
`mtu`.`user_id` AS `user_id`, | |||
`mtu`.`id` AS `mtu_id`, | |||
`mw`.`id` AS `mw_id` | |||
FROM | |||
`md_work` AS `mw` | |||
LEFT JOIN `md_team` AS `mt` ON `mw`.`team_id` = `mt`.`id` | |||
LEFT JOIN `md_team_user` AS `mtu` ON `mtu`.`team_id` = `mw`.`team_id` | |||
LEFT JOIN `md_user` AS `mu` ON `mtu`.`user_id` = `mu`.`id` | |||
WHERE | |||
`mtu`.`role_id` = 1 | |||
ORDER BY | |||
`mw`.`id`; | |||
</syntaxhighlight> | |||
2022年2月7日 (一) 09:17的版本
引言
常用查询方式
SELECT *
FROM `table1`
INNER JOIN `table2` ON `table1`.xxx_id = `table2`.id;
联查
SELECT
adh.id,
adh.name,
adh.team_id,
adh.work_id,
adh.group_id,
adh.history_id,
adh.resource,
adh.format,
adh.x,
adh.y,
adh.width,
adh.height,
t.name AS t_name,
w.name AS w_name,
u.name AS u_name,
indah.name AS history_history_name,
indah.id AS history_history_id,
indah.create_time AS history_create_time,
indah.version AS history_version,
indah.message AS history_message,
dah.create_time,
dah.version,
dah.message
FROM
`md_design_artboard` adh
LEFT JOIN `md_design_artboard_history` `dah` ON `adh`.`history_id` = `dah`.`id`
LEFT JOIN `md_design_artboard_history` `indah`
ON `adh`.`id` = `indah`.`artboard_id`
LEFT JOIN `md_user` `u`
ON dah.user_id = `u`.`id`
LEFT JOIN `md_team` `t`
ON `adh`.`team_id` = `t`.`id`
LEFT JOIN `md_work` `w`
ON `adh`.`work_id` = `w`.`id`
WHERE
adh.team_id = '13'
AND adh.work_id = '131'
ORDER BY adh.name;
统计部门下属项目
SELECT
`mw`.`id` AS `id`,
`mw`.`name` AS `name`,
`mu`.`department` AS `department`,
`mtu`.`role_id` AS `role_id`,
`mtu`.`user_id` AS `user_id`,
`mtu`.`id` AS `mtu_id`,
`mw`.`id` AS `mw_id`
FROM
`md_work` AS `mw`
LEFT JOIN `md_team` AS `mt` ON `mw`.`team_id` = `mt`.`id`
LEFT JOIN `md_team_user` AS `mtu` ON `mtu`.`team_id` = `mw`.`team_id`
LEFT JOIN `md_user` AS `mu` ON `mtu`.`user_id` = `mu`.`id`
WHERE
`mtu`.`role_id` = 1
ORDER BY
`mw`.`id`;
No categories