我需要从表中选择一些列,还需要根据一列中的值选择其他列。例如:
SELECT
col_a, col_b, col_c
FROM user_table
INNER JOIN col_d, col_e, col_f ON col_type = 1
INNER JOIN col_g, col_h, col_i ON col_type = 2
WHERE id = :id
AND active = 1
由于我是mysql的新手,我不知道应该如何做到这一点。我尝试使用CASE WHEN
,但能够选择一个列,我需要选择多个列。
答案 0 :(得分:0)
我认为你误解了INNER JOIN,你想要那样的东西
SELECT
col_a, col_b, col_c,
IF( col_type = 1, col_d, NULL ) AS col_d,
IF( col_type = 1, col_e, NULL ) AS col_e,
IF( col_type = 2, col_g, NULL ) AS col_g,
IF( col_type = 2, col_h, NULL ) AS col_h
FROM user_table
WHERE id = :id
AND active = 1
但我更喜欢加载所有cols并用你的其他语言处理它(PHP?)。