我有以下查询,它是由PHP函数生成的,它变得非常复杂但到目前为止工作正常。
如果第三个字段等于1,我想要做的是通过第二个字段覆盖一个字段。
所以当 table4 .cpda_meta_val等于1时,我希望 table2 .cpda_meta_val实际上等于 table3 .cpda_meta_val,这样我就可以了在WHERE / ORDER BY子句中使用结果值table2.cpda_meta_val。
我希望到目前为止我已经有道理了。
以下是原始查询:
SELECT
mainTable.cpda_id
FROM
cp_data AS mainTable
LEFT OUTER JOIN
cp_data AS table0
ON table0.cpda_meta_id = mainTable.cpda_id
AND table0.cpda_meta_key = '47248'
LEFT OUTER JOIN
cp_data AS table1
ON table1.cpda_meta_id = mainTable.cpda_id
AND table1.cpda_meta_key = '47241'
LEFT OUTER JOIN
cp_data AS table2
ON table2.cpda_meta_id = mainTable.cpda_id
AND table2.cpda_meta_key = '47242'
LEFT OUTER JOIN
cp_data AS table3
ON table3.cpda_meta_id = mainTable.cpda_id
AND table3.cpda_meta_key = '66838'
LEFT OUTER JOIN
cp_data AS table4
ON table4.cpda_meta_id = mainTable.cpda_id
AND table4.cpda_meta_key = '66843'
LEFT OUTER JOIN
cp_data AS table5
ON table5.cpda_meta_id = mainTable.cpda_id
AND table5.cpda_meta_key = '47252'
LEFT OUTER JOIN
cp_data AS table6
ON table6.cpda_meta_id = mainTable.cpda_id
AND table6.cpda_meta_key = '47255'
WHERE
mainTable.cpda_meta_id = 47236
AND mainTable.cpda_active = 1
AND mainTable.cpda_meta_key = 'set_element_stub'
AND table0.cpda_meta_val = 49297
AND table1.cpda_meta_val != 66116
AND (
table5.cpda_meta_val = 497
OR table6.cpda_meta_val = 497
)
ORDER BY
table2.cpda_meta_val,
mainTable.cpda_seq,
mainTable.cpda_id
这是我尝试实现上述目标,但不起作用并且不检索任何行:
SELECT
mainTable.cpda_id
FROM
cp_data AS mainTable
LEFT OUTER JOIN
cp_data AS table0
ON table0.cpda_meta_id = mainTable.cpda_id
AND table0.cpda_meta_key = '47248'
LEFT OUTER JOIN
cp_data AS table1
ON table1.cpda_meta_id = mainTable.cpda_id
AND table1.cpda_meta_key = '47241'
LEFT OUTER JOIN
cp_data AS table2
ON table2.cpda_meta_id = mainTable.cpda_id
AND table2.cpda_meta_key = '47242'
LEFT OUTER JOIN
cp_data AS table3
ON table3.cpda_meta_id = mainTable.cpda_id
AND table3.cpda_meta_key = '66838'
LEFT OUTER JOIN
cp_data AS table4
ON table4.cpda_meta_id = mainTable.cpda_id
AND table4.cpda_meta_key = '66843'
LEFT OUTER JOIN
cp_data AS table5
ON table5.cpda_meta_id = mainTable.cpda_id
AND table5.cpda_meta_key = '47252'
LEFT OUTER JOIN
cp_data AS table6
ON table6.cpda_meta_id = mainTable.cpda_id
AND table6.cpda_meta_key = '47255'
CASE
WHEN table4.cpda_meta_val = 1 THEN table3.cpda_meta_val
ELSE table2.cpda_meta_val
END as table2.cpda_meta_val
WHERE
mainTable.cpda_meta_id = 47236
AND mainTable.cpda_active = 1
AND mainTable.cpda_meta_key = 'set_element_stub'
AND table0.cpda_meta_val = 49297
AND table1.cpda_meta_val != 66116
AND (
table5.cpda_meta_val = 497
OR table6.cpda_meta_val = 497
)
ORDER BY
table2.cpda_meta_val,
mainTable.cpda_seq,
mainTable.cpda_id
如果有人能指出我正确的方向,那就太好了!谢谢!
编辑 - 对于那些搜索,我使用下面的戈登答案进行以下查询,现在可以使用:
SELECT
mainTable.cpda_id,
(CASE
WHEN table4.cpda_meta_val = 1 THEN table3.cpda_meta_val
ELSE table2.cpda_meta_val
END) as override_val_0
FROM
cp_data AS mainTable
LEFT OUTER JOIN
cp_data AS table0
ON table0.cpda_meta_id = mainTable.cpda_id
AND table0.cpda_meta_key = '47248'
LEFT OUTER JOIN
cp_data AS table1
ON table1.cpda_meta_id = mainTable.cpda_id
AND table1.cpda_meta_key = '47241'
LEFT OUTER JOIN
cp_data AS table2
ON table2.cpda_meta_id = mainTable.cpda_id
AND table2.cpda_meta_key = '47242'
LEFT OUTER JOIN
cp_data AS table3
ON table3.cpda_meta_id = mainTable.cpda_id
AND table3.cpda_meta_key = '66838'
LEFT OUTER JOIN
cp_data AS table4
ON table4.cpda_meta_id = mainTable.cpda_id
AND table4.cpda_meta_key = '66843'
LEFT OUTER JOIN
cp_data AS table5
ON table5.cpda_meta_id = mainTable.cpda_id
AND table5.cpda_meta_key = '47252'
LEFT OUTER JOIN
cp_data AS table6
ON table6.cpda_meta_id = mainTable.cpda_id
AND table6.cpda_meta_key = '47255'
WHERE
mainTable.cpda_meta_id = 47236
AND mainTable.cpda_active = 1
AND mainTable.cpda_meta_key = 'set_element_stub'
AND table0.cpda_meta_val = 49297
AND table1.cpda_meta_val != 66116
AND (
table5.cpda_meta_val = 497
OR table6.cpda_meta_val = 497
)
ORDER BY
override_val_0,
mainTable.cpda_seq,
mainTable.cpda_id
答案 0 :(得分:1)
case
语句属于select
子句:
SELECT mainTable.cpda_id,
(CASE WHEN table4.cpda_meta_val = 1 THEN table3.cpda_meta_val
ELSE table2.cpda_meta_val
END) as cpda_meta_val
您也可以将此表达式直接放在order by
中(这似乎是您使用table2_cpda_meta_val
的唯一地方:
ORDER BY (CASE WHEN table4.cpda_meta_val = 1 THEN table3.cpda_meta_val
ELSE table2.cpda_meta_val
END),
mainTable.cpda_seq,
mainTable.cpda_id