如何在mysql查询的条件语句中调用子查询

时间:2013-09-05 10:48:40

标签: php mysql

我有两个表table1和table2。当status = 1

时,我需要table1的值
    SELECT type, no, 
        CASE WHEN status = '0' THEN 'Vacant'
             WHEN status = '2' THEN 'Repair'
             ELSE (SELECT `field1` FROM `table1` WHERE `field2`=10) AS test END AS addr 
FROM table2

此查询以#1064

输出错误

如何在单个MYSQL查询中获取值?

2 个答案:

答案 0 :(得分:1)

您可以使用子查询:

SELECT * FROM t1
WHERE s11 > ANY
 (SELECT COUNT(*) /* no hint */ FROM t2
 WHERE NOT EXISTS
  (SELECT * FROM t3
   WHERE ROW(5*t2.s1,77)=
    (SELECT 50,11*s1 FROM t4 UNION SELECT 50,77 FROM
     (SELECT * FROM t5) AS t5)));

答案 1 :(得分:1)

SELECT type, no, 
    CASE WHEN status = '0' THEN 'Vacant'
         WHEN status = '2' THEN 'Repair'
         WHEN status = '1' (SELECT `field1` FROM `table1` WHERE `field2`=10) END AS addr FROM table2

如果您尝试此代码,那么您将不会收到错误