0

我正在尝试创建一个 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”和“Value”。我想按“ID”搜索并返回“Value”。表名是 cable_length 等。非常感谢任何帮助。

4

3 回答 3

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

如果没有保证存在任何值,则破解方法是有一个子查询,它是UNION ALL8 个查询中的一个,它返回 8 列,只有一个填充,然后选择MAX每列的作为该列的名称。

这是一个糟糕的技巧,我过去曾使用过它来避免许多只偶尔加入的左连接的性能问题。

顺便说一句,名为 A1..A8 的 8 个表表明您需要有数据库经验的人来查看您的设计并为您提供更好的布局。

于 2012-06-27T15:01:01.623 回答
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;
于 2012-06-27T15:01:57.253 回答
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
);
于 2012-06-27T15:12:10.543 回答