有没有办法有效地改进下面的代码而不重复这样的查询?
注意:在这种情况下,有一些已知数量的元素可用于分配参数。
SELECT @var0 = Field_A FROM Table_0
JOIN Table_1 ON (Table_0.PK = Table_1.FK)
WHERE Table_1.PK = 1 AND TABLE_0.Field_B = 1;
SELECT @var1 = Field_A FROM Table_0
JOIN Table_1 ON (Table_0.PK = Table_1.FK)
WHERE Table_1.PK = 1 AND TABLE_0.Field_B = 2;
SELECT @var2 = Field_A FROM Table_0
JOIN Table_1 ON (Table_0.PK = Table_1.FK)
WHERE Table_1.PK = 1 AND TABLE_0.Field_B = 3;
答案 0 :(得分:1)
这样的事情:
SELECT
@var0 = MAX( CASE table_0.field_b WHEN 1 THEN field_a END )
, @var1 = MAX( CASE table_0.field_b WHEN 2 THEN field_a END )
, @var2 = MAX( CASE table_0.field_b WHEN 3 THEN field_a END )
FROM
table_0
JOIN
table_1
ON
TABLE_0.PK = TABLE_1.FK