繁体   English   中英

MySQL为null不返回任何内容

[英]MySQL is null doesn't return anything

我有以下查询:

select 
    emp.name, emp.birthdate, emp.ssn, emp.current_employee, 
    german.introduction as "German Intro", german.work_experience as "German Work Experience", german.education as "German Education",
    chinese.introduction as "Chinese Intro", chinese.work_experience as "Chinese Work Experience", chinese.education as "Chinese Education"
from employees emp
left join contact_info german on emp.id = german.id_employee
left join contact_info chinese on emp.id = chinese.id_employee
where emp.name like '%peter%' 
and (german.id_language = 1 or german.id_language IS NULL)
and (chinese.id_language = 2 or chinese.id_language IS NULL)

表的创建查询:

CREATE TABLE `contact_info` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `id_employee` int(11) NOT NULL,
  `id_language` int(11) NOT NULL,
  `introduction` text,
  `work_experience` text,
  `education` text,
  PRIMARY KEY (`id`)
) 
CREATE TABLE `employees` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL DEFAULT '',
  `birthdate` date NOT NULL,
  `ssn` varchar(20) NOT NULL DEFAULT '',
  `current_employee` enum('Y','N') NOT NULL DEFAULT 'Y',
  PRIMARY KEY (`id`)
)
CREATE TABLE `languages` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `language_name` varchar(255) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) 

我在员工日志中确实有Peter的记录。 在contact_info表中有一行包含德语文本,但在contact_info表中无一行包含中文文本

我希望获得有关Peter的所有信息,其中包含德语文本,而中文文本为NULL,但是最终没有结果。 知道我缺少什么吗?

额外信息:语言表包含3种语言:德语,中文和英语,将来可能会添加更多语言。 如果可以通过某种方式设置此查询,使其立即自动加载所有语言,那就太棒了

我认为您需要这样做,并将id_language添加到您的加入条件中。

select 
    emp.name, emp.birthdate, emp.ssn, emp.current_employee, 
    german.introduction as "German Intro", german.work_experience as "German Work Experience", german.education as "German Education",
    chinese.introduction as "Chinese Intro", chinese.work_experience as "Chinese Work Experience", chinese.education as "Chinese Education"
from employees emp
left join contact_info german on emp.id = german.id_employee AND german.id_language = 1
left join contact_info chinese on emp.id = chinese.id_employee AND chinese.id_language = 2
where emp.name like '%peter%' 
and (german.id_language = 1 or german.id_language IS NULL)
and (chinese.id_language = 2 or chinese.id_language IS NULL)

当您使用left join ,条件(通常)应on而不是where 所以:

select emp.name, emp.birthdate, emp.ssn, emp.current_employee, 
       german.introduction as "German Intro", german.work_experience as "German Work Experience", german.education as "German Education",
       chinese.introduction as "Chinese Intro", chinese.work_experience as "Chinese Work Experience", chinese.education as "Chinese Education"
from employees emp left join
     contact_info german 
     on emp.id = german.id_employee and german.id_language = 1 left join 
     contact_info chinese
     on emp.id = chinese.id_employee and chinese.id_language = 2
where emp.name like '%peter%' ;

where包含条件是多余的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM