创建具有空关系的MYSQL查询

问题描述 投票:0回答:2

假设我有下表,其中每个表中的id是主键,第二个中的actor是第一个的外键。

 table: actors
 id |              name              
----+--------------------------------
  1 | Paul Rudd            
  2 | Danny Devito             
  3 | Mark Ruffalo 
  4 | David Allen Grier      

table: movies
 id |     name      | actors
----+---------------+----------------
  1 | So this is 40 |              1
  2 | Avengers      |              3
  3 | Twins         |              2

我需要编写一个sql查询,该查询将选择所有有电影的电影名称以及他们的演员和没有电影的演员

这是我所拥有的:

SELECT IFNULL(m.name, ‘none’), a.name
FROM actors a
JOIN movies m
ON m.id = a.id;

我得到:

ERROR:  column "‘none’" does not exist
LINE 1: SELECT IFNULL(m.name, ‘none’), a.name
sql mysqli null relationship
2个回答
0
投票

尝试“无”而不是“无”。


0
投票

对我来说很好,请检查here。您在查询中没有使用正确的单引号'

SELECT 
    IFNULL(m.name, 'none'), a.name
FROM actors a
JOIN movies m
ON m.id = a.id;
© www.soinside.com 2019 - 2024. All rights reserved.