【问题标题】:Mysql Inner Join a table with matching key column but nullMysql Inner 加入具有匹配键列但为空的表
【发布时间】:2023-03-29 05:22:01
【问题描述】:

我正在尝试加入 4 个表。

registration_mtadmission_mtstudent_mtschoolyear_student_lt

目前registration_mtadmission_mtstudent_mt 表各有1 条记录。 没有任何记录的schoolyear_student_lt 的内部连接所有三个除了 没有问题。

即使匹配键 student_id HAS OR HAS-,我也希望能够获取 schoolyear_student_lt 的列并将其与其他 3 个表的结果集连接起来没有记录

我想将schoolyear_idstudent_idgradelevel_idsection_idpassedfrom schoolyear_student_lt 添加到我从联接查询where isActive = 0 获得的结果集中;

CREATE 表语句

CREATE TABLE `registration_mt` (
  `registration_id` int(11) NOT NULL AUTO_INCREMENT,
  `student_type` varchar(45) NOT NULL,
  PRIMARY KEY (`registration_id`)
) ;

CREATE TABLE `admission_mt` (
  `admission_id` int(11) NOT NULL AUTO_INCREMENT,
  `registration_id` int(11) NOT NULL,
  `isComplete` bit(1) NOT NULL DEFAULT b'0',
  `completion_date` datetime DEFAULT NULL,
  PRIMARY KEY (`admission_id`),
  UNIQUE KEY `registration_id_UNIQUE` (`registration_id`),
  CONSTRAINT `fk_admission_mtTABLE_registration_idCOL` FOREIGN KEY (`registration_id`) REFERENCES `registration_mt` (`registration_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ;

CREATE TABLE `schoolyear_student_lt` (
  `schoolyear_id` int(11) NOT NULL,
  `student_id` int(11) NOT NULL,
  `gradelevel_id` int(11) NOT NULL,
  `section_id` int(11) DEFAULT NULL,
  `passed` bit(1) DEFAULT b'0',
  UNIQUE KEY `uk_schoolyear_idCOL_student_idCOL` (`schoolyear_id`,`student_id`)
);

CREATE TABLE `student_mt` (
  `student_id` int(11) NOT NULL AUTO_INCREMENT,
  `registration_id` int(11) NOT NULL,
  `entry_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `isGraduated` bit(1) NOT NULL DEFAULT b'0',
  `date_graduated` datetime DEFAULT NULL,
  `isActive` bit(1) DEFAULT b'0' ,
  PRIMARY KEY (`student_id`),
  UNIQUE KEY `registration_id_UNIQUE` (`registration_id`),
  KEY `fk_student_mtTABLE_registration_idCOL_idx` (`registration_id`),
  CONSTRAINT `fk_student_mtTABLE_registration_idCOL` FOREIGN KEY (`registration_id`) REFERENCES `registration_mt` (`registration_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
);

我的INNER-JOIN 声明

SELECT 

a.admission_id, 
a.isComplete, 
a.completion_date,
s.student_id,
s.entry_date,
s.isGraduated, 
s.date_graduated, 
s.isActive

FROM admission_mt a 
INNER JOIN registration_mt r ON a.registration_id = r.registration_id
INNER JOIN student_mt s ON s.registration_id = a.registration_id
-- INNER JOIN schoolyear_student_lt sslt ON s.student_id = sslt.student_id
-- LEFT JOIN schoolyear_student_lt sslt ON s.student_id = sslt.student_id 


 WHERE 

 s.isActive = 0 ;

INSERTregistration_mtadmission_mtstudent_mt 表的语句

START TRANSACTION;

INSERT INTO registration_mt(student_type) VALUES('New'); -- insert to registration_mt

INSERT INTO admission_mt(registration_id)
VALUES(LAST_INSERT_ID()); --insert to admissiont_mt

COMMIT;

START TRANSACTION;

UPDATE admission_mt 
SET isComplete = 1 
WHERE registration_id = 1;

INSERT INTO student_mt(registration_id)
VALUES(1); --insert to student_mt

COMMIT;

我注释掉了 -- INNER JOIN schoolyear_student_lt sslt ON s.student_id = sslt.student_id,因为当我将它包含在我的选择查询中时我没有得到任何结果,因为 schoolyear_student_lt 表中还没有记录。

这是我得到的当前结果集。 schoolyear_student_lt 表中没有我希望添加的列,即使在 NULLNOT NULLschoolyear_student_lt.student_id

LEFT-JOIN 不显示来自 schoolyear_student_lt 的列,而 NULL

谢谢。

【问题讨论】:

  • LEFT JOIN 怎么样?
  • 如果你想加入一个表并包含在schoolyear_student_lt中没有匹配行的记录,那么根据定义这是一个outer join。所以使用正确的连接类型。
  • @jarlh 是的,我试过了,但没有用。我得到了与屏幕截图相同的结果集。
  • @piet.t 我尝试了 LEFT JOIN / LEFT OUTER JOIN 但得到的结果与屏幕截图类似。
  • 请显示您在使用left join 时尝试的确切语句 - 包括选择列表以及注释或未注释的内容。

标签: mysql sql join inner-join


【解决方案1】:

你应该试试LEFT OUTER JOIN

左外连接的想法是,如果没有连接,它将获取左关系的行并用NULL 填充右关系的字段 合作伙伴。

还有一个右外连接...

【讨论】:

  • 感谢您的参考链接。
猜你喜欢
  • 2015-08-13
  • 1970-01-01
  • 2017-05-02
  • 1970-01-01
  • 2014-12-23
  • 2012-06-30
  • 1970-01-01
  • 2023-04-06
  • 2021-12-30
相关资源
最近更新 更多