SQL OUTER JOIN与空字段(null),其中没有结果

时间:2015-03-08 12:16:20

标签: mysql outer-join

Hulloes SO!

好的,我的情况是这样的:我有一个数据库,我需要从4个不同的表中进行SELECT,得到结果,其中一个表中的foregin键与我其他3个表中的id匹配。

结构:

CREATE TABLE IF NOT EXISTS `languages` (
  `id` int(11) NOT NULL,
  `lang_name` varchar(64) COLLATE utf8_bin NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

CREATE TABLE IF NOT EXISTS `main_keys` (
  `id` int(11) NOT NULL,
  `project` int(11) NOT NULL,
  `key` text COLLATE utf8_bin NOT NULL,
  `comment` varchar(128) COLLATE utf8_bin NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

CREATE TABLE IF NOT EXISTS `projects` (
  `id` int(11) NOT NULL,
  `project_name` varchar(64) COLLATE utf8_bin NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

CREATE TABLE IF NOT EXISTS `translations` (
  `id` int(11) NOT NULL,
  `project` int(11) NOT NULL,
  `key` int(11) NOT NULL,
  `language` int(11) NOT NULL,
  `translation` text COLLATE utf8_bin NOT NULL,
  `lastedit` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

translations表是包含所有关系的表格(translations.project => project.idtranslations.key => main_keys.id和{{1 }} => translations.language)。

Relational overview http://assets.brianemilius.com/locale_relations.png

我当前的查询看起来像这样

language.id

我从特定项目(p.id)和特定语言(l.id)中选择,我想要一个结果,显示如下:

SELECT t.id, mk.key, t.translation, mk.comment, p.project_name, t.lastedit
    FROM translations t
    INNER JOIN languages l ON l.id = t.language
    INNER JOIN main_keys mk ON mk.id = t.key
    INNER JOIN projects p ON p.id = mk.project
    WHERE p.id = '1' AND l.id = '2'

第2行和第3行表示没有翻译的结果。

但我只得到1行结果 - 顶部的结果为id 2(因为目前表+----+--------------+-------------+---------------------------+-----------------------+---------------------+ | id | key | translation | comment | project_name | lastedit | +----+--------------+-------------+---------------------------+-----------------------+---------------------+ | 2 | Hello World! | Hej verden! | Test key for dev purposes | Locale Administrator | 2015-02-24 12:37:28 | +----+--------------+-------------+---------------------------+-----------------------+---------------------+ | | test key | | Test key for dev purposes | Locale Administrator | | +----+--------------+-------------+---------------------------+-----------------------+---------------------+ | | test key 2 | | Test key for dev purposes | Locale Administrator | | +----+--------------+-------------+---------------------------+-----------------------+---------------------+ 中只有一个翻译行)。

我在我的查询中尝试了LEFT和RIGHT JOIN的各种组合,但似乎没有任何帮助。我是在轨道还是我需要完全重新思考?

1 个答案:

答案 0 :(得分:2)

我认为您需要重新考虑一下并使用main_keys表作为主要来源,然后加入其他表:

SELECT t.id, mk.key, t.translation, mk.comment, p.project_name, t.lastedit
FROM main_keys mk
LEFT JOIN translations t ON mk.id = t.key
LEFT JOIN languages l ON l.id = t.language  
LEFT JOIN projects p ON p.id = mk.project 
WHERE p.id = 1 AND l.id = 2 OR l.id IS null;