0

有没有办法在不重复这样的查询的情况下有效地改进下面的代码?

注意:在这种情况下,有已知数量的元素用于分配参数。

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;
4

1 回答 1

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
于 2013-05-03T15:32:06.837 回答