我正在尝试创建一个带有八个不同数字的SQL语句,并从一个数据库中搜索八个不同的表以获取相应的值。我一直得到无效的语法,但我无法弄清楚为什么。这是我的代码:
SELECT cable_length.ID, drawing.ID, optional.ID, output_type.ID, pressure_range.ID, pressure_type.ID, series.ID, termination.ID
FROM
(
SELECT value AS cable_length FROM A1 WHERE A1.id = %s
JOIN SELECT value AS drawing FROM A2 WHERE A2.id = %s,
JOIN SELECT value AS optional FROM A3 WHERE A3.id = %s,
JOIN SELECT value AS output_type FROM A4 WHERE A4.id = %s,
JOIN SELECT value AS pressure_range FROM A5 WHERE A5.id = %s,
JOIN SELECT value AS pressure_type FROM A6 WHERE A6.id = %s,
JOIN SELECT value AS series FROM A7 WHERE A7.id = %s,
JOIN SELECT value AS termination FROM A8 WHERE A8.id = %s
);
%s将仅更改为数字。每个表中的列名是“ID”和“值”。我想按“ID”搜索并返回“Value”。表名是cable_length等。非常感谢任何帮助。
答案 0 :(得分:2)
如果你知道所有8个值都存在,你可以像这样加入:
SELECT A1.value as cable_length
, A2.value as drawing
, A3.value as optional
, A4.value as output_type
, A5.value as pressure_range
, A6.value as pressure_type
, A7.value as series
, A7.value as termination
FROM A1, A2, A3, A4, A5, A6, A7, A8
WHERE A1.ID = %s
AND A2.ID = %s
AND A3.ID = %s
AND A4.ID = %s
AND A5.ID = %s
AND A6.ID = %s
AND A7.ID = %s
AND A8.ID = %s
如果您不知道这一点,但保证第一个值存在,那么您的查询必须变得更加丑陋,因为您需要左连接。
SELECT A1.value as cable_length
, A2.value as drawing
, A3.value as optional
, A4.value as output_type
, A5.value as pressure_range
, A6.value as pressure_type
, A7.value as series
, A7.value as termination
FROM A1
LEFT JOIN A2
ON A2.ID = %s
LEFT JOIN A3
ON A3.ID = %s
LEFT JOIN A4
ON A4.ID = %s
LEFT JOIN A5
ON A5.ID = %s
LEFT JOIN A6
ON A6.ID = %s
LEFT JOIN A7
ON A7.ID = %s
LEFT JOIN A8
ON A8.ID = %s
WHERE A1.ID = %s
如果没有任何值保证存在,那么hack的子查询是8个查询的UNION ALL
,返回8列,只有一个填充,然后选择每个MAX
列作为该列名。
这是我过去用过的一个非常糟糕的技巧,可以避免许多只偶尔加入的左连接的性能问题。
顺便说一下,名为A1..A8的8个表格表明,您需要具有数据库经验的人来查看您的设计并为您提供更好的布局。
答案 1 :(得分:0)
也许这就是你想要的。也许不是,我只是重写了你的问题:
SELECT
cable_length.ID,
drawing.ID,
optional.ID,
output_type.ID,
pressure_range.ID,
pressure_type.ID,
series.ID,
termination.ID
FROM (SELECT ID FROM A1 WHERE A1.id = %s) AS cable_length
JOIN (SELECT ID FROM A2 WHERE A2.id = %s) AS drawing
JOIN (SELECT ID FROM A3 WHERE A3.id = %s) AS optional
JOIN (SELECT ID FROM A4 WHERE A4.id = %s) AS output_type
JOIN (SELECT ID FROM A5 WHERE A5.id = %s) AS pressure_range
JOIN (SELECT ID FROM A6 WHERE A6.id = %s) AS pressure_type
JOIN (SELECT ID FROM A7 WHERE A7.id = %s) AS series
JOIN (SELECT ID FROM A8 WHERE A8.id = %s) AS termination;
答案 2 :(得分:0)
UNION ALL将在这种情况下工作,但它可能不是最好的解决方案,更不用说乱七八糟了(参见@btilly提供的答案)。另请注意,这是SQL Server语法,可能与MySql略有不同
select max(cable_length), max(drawing), max(optional), max(output_type), max(pressure_range), max(pressure_type), max(series), max(termination)
from
(
SELECT value as cable_length, null, null, null, null, null, null, null, null
FROM cable_length WHERE id = %s
UNION ALL
SELECT null, value as drawing, null, null, null, null, null, null, null
FROM drawing WHERE id = %s
UNION ALL
SELECT null, null, value as optional, null, null, null, null, null, null
FROM optional WHERE id = %s
UNION ALL
SELECT null, null, null, value as output_type, null, null, null, null, null
FROM output_type WHERE id = %s
UNION ALL
SELECT null, null, null, null, value as pressure_range, null, null, null
FROM pressure_range WHERE id = %s
UNION ALL
SELECT null, null, null, null, null, value as pressure_type, null, null
FROM pressure_type WHERE id = %s
UNION ALL
SELECT null, null, null, null, null, null, value as series, null
FROM series WHERE id = %s
UNION ALL
SELECT null, null, null, null, null, null, null, value as termination
FROM termination WHERE id = %s
);