mysql:没有找到记录时如何返回NULL结果?

时间:2013-03-17 20:26:48

标签: mysql

这是我正在做的MySQL:

mysql> select "Le Café de la Mairie" as titre, "15 pl Mairie" as adresse1, id from ville where sound=soundex('FERTE SOUS JOUARRE') and cp='77260';
+-----------------------+--------------+-----+
| titre                 | adresse1     | id  |
+-----------------------+--------------+-----+
| Le Café de la Mairie  | 15 pl Mairie | 447 |
+-----------------------+--------------+-----+
1 row in set (0.00 sec)

mysql> select "Le Café de la Mairie" as titre, "15 pl Mairie" as adresse1, id from ville where sound=soundex('FERTE SOUS JOUARRE') and cp='77261';
Empty set (0.00 sec)

mysql>

这就是我想要的:

mysql> select "Le Café de la Mairie" as titre, "15 pl Mairie" as adresse1, id from ville where sound=soundex('FERTE SOUS JOUARRE') and cp='77260';
+-----------------------+--------------+-----+
| titre                 | adresse1     | id  |
+-----------------------+--------------+-----+
| Le Café de la Mairie  | 15 pl Mairie | 447 |
+-----------------------+--------------+-----+
1 row in set (0.00 sec)

mysql> **(same query but dont find a record)**;
+--------------+------------+------+
| titre        | adresse1   | id   |
+--------------+------------+------+
| NULL         | NULL       | NULL |
+--------------+------------+------+

mysql>

当然,我已经google& search stackoverflow。

1 个答案:

答案 0 :(得分:1)

SELECT
    t.*
FROM 
    (SELECT 1) AS dummy
  LEFT JOIN
    (SELECT ... your select here) AS t
      ON TRUE ;

所以,坚持这个问题:

SELECT
    t.*
FROM 
    (SELECT 1) AS dummy
LEFT JOIN
    ( SELECT
        "Le Café de la Mairie" as titre,
        "15 pl Mairie" as adresse1,
        id
      FROM ville
      WHERE sound=soundex('FERTE SOUS JOUARRE')
      AND cp='77261') AS t
ON TRUE;