SQL 查询 - 使条件成为条件?
SQL query - make conditions conditional?
我有两个 table(这些只是简化版本):
`Locations`:
id city country area
-- ---- ------- ----
71 C X E
72 C B E
73 C F G
74 L X E
75 M N O
76 M N E
`Findings`:
ID city country area resultVM
-- ---- ------- ---- --------
18 empty
19 C lam
20 X lam8
21 E pdg4
现在,我想根据这两个中的数据写一个填充第三个 table 的插入。它应该包含来自 Location table 的所有 ID(Summed.locations_id 显然是对 Locations.id 的引用),以及来自 Findings table 的匹配结果VM。结果应该是:
`Summed`:
locations_id resultVM
------------ --------
71 lam
72 lam
73 lam
74 lam8
75 empty
76 pdg4
标准是:首先尝试匹配城市以在Findings table中找到结果VM,如果没有结果则匹配国家,然后是地区,最后如果根本没有结果然后是没有 city/country/area 的空行。
目前我在重复键上使用 4 inserts/updates 来执行此操作,但由于 table 非常大,需要花费大量时间。有谁知道这是否可以在单个 update/insert/anything 中完成(在 MySQL 中)?
谢谢!
你可以左连接。
INSERT INTO Summed (locations_id,resultVM)
SELECT Locations.`id` AS locations_id,(CASE WHEN Findings_city.`city` IS NOT NULL THEN Findings_city.`resultVM`
WHEN Findings_country.`country` IS NOT NULL THEN Findings_country.`resultVM`
WHEN Findings_area.`area` IS NOT NULL THEN Findings_area.`resultVM`
ELSE 'empty'END) AS resultVM
FROM
Locations
LEFT JOIN Findings Findings_city ON Locations.`city` = Findings_city.`city`
LEFT JOIN Findings Findings_country ON Locations.`country` = Findings_country.`country`
LEFT JOIN Findings Findings_area ON Locations.`area` = Findings_area.`area`
GROUP BY Locations.`id`
这里是SQL Fiddle Demo
希望这有帮助..
尝试:
INSERT INTO `Summed`
SELECT
`l`.`id`,
COALESCE(
`fcity`.`resultVM`,
`fcountry`.`resultVM`,
`farea`.`resultVM`,
`fempty`.`resultVM`)
FROM
`Locations` `l`
LEFT JOIN `Findings` `fcity` ON `l`.`city` = `fcity`.`city`
LEFT JOIN `Findings` `fcountry` ON `l`.`country` = `fcountry`.`country`
LEFT JOIN `Findings` `farea` ON `l`.`area` = `farea`.`area`
LEFT JOIN `Findings` `fempty` ON `fempty`.`city` = '' AND
`fempty`.`country` = '' AND
`fempty`.`area` = '';
我有两个 table(这些只是简化版本):
`Locations`:
id city country area
-- ---- ------- ----
71 C X E
72 C B E
73 C F G
74 L X E
75 M N O
76 M N E
`Findings`:
ID city country area resultVM
-- ---- ------- ---- --------
18 empty
19 C lam
20 X lam8
21 E pdg4
现在,我想根据这两个中的数据写一个填充第三个 table 的插入。它应该包含来自 Location table 的所有 ID(Summed.locations_id 显然是对 Locations.id 的引用),以及来自 Findings table 的匹配结果VM。结果应该是:
`Summed`:
locations_id resultVM
------------ --------
71 lam
72 lam
73 lam
74 lam8
75 empty
76 pdg4
标准是:首先尝试匹配城市以在Findings table中找到结果VM,如果没有结果则匹配国家,然后是地区,最后如果根本没有结果然后是没有 city/country/area 的空行。
目前我在重复键上使用 4 inserts/updates 来执行此操作,但由于 table 非常大,需要花费大量时间。有谁知道这是否可以在单个 update/insert/anything 中完成(在 MySQL 中)? 谢谢!
你可以左连接。
INSERT INTO Summed (locations_id,resultVM)
SELECT Locations.`id` AS locations_id,(CASE WHEN Findings_city.`city` IS NOT NULL THEN Findings_city.`resultVM`
WHEN Findings_country.`country` IS NOT NULL THEN Findings_country.`resultVM`
WHEN Findings_area.`area` IS NOT NULL THEN Findings_area.`resultVM`
ELSE 'empty'END) AS resultVM
FROM
Locations
LEFT JOIN Findings Findings_city ON Locations.`city` = Findings_city.`city`
LEFT JOIN Findings Findings_country ON Locations.`country` = Findings_country.`country`
LEFT JOIN Findings Findings_area ON Locations.`area` = Findings_area.`area`
GROUP BY Locations.`id`
这里是SQL Fiddle Demo 希望这有帮助..
尝试:
INSERT INTO `Summed`
SELECT
`l`.`id`,
COALESCE(
`fcity`.`resultVM`,
`fcountry`.`resultVM`,
`farea`.`resultVM`,
`fempty`.`resultVM`)
FROM
`Locations` `l`
LEFT JOIN `Findings` `fcity` ON `l`.`city` = `fcity`.`city`
LEFT JOIN `Findings` `fcountry` ON `l`.`country` = `fcountry`.`country`
LEFT JOIN `Findings` `farea` ON `l`.`area` = `farea`.`area`
LEFT JOIN `Findings` `fempty` ON `fempty`.`city` = '' AND
`fempty`.`country` = '' AND
`fempty`.`area` = '';