我对我的 mysql 数据库有以下查询。
SELECT
mn.id,
mn.created_date,
mn.created_by,
mnt.description,
u.handle,
mn.admin_request_company_id
FROM member_notification mn,
member_notification_type mnt,
member m,
user u
WHERE mn.member_notification_type_id = mnt.id
AND mnt.name = 'COMPANYVALIDATION'
AND mn.created_by = m.id
AND m.user_id = u.id;
这工作正常,但是我现在需要扩展它以从另一个表中获取公司名称。问题是,要做到这一点,我需要使用键 mn.admin_request_company_id 重新加入成员表,但是我不知道该怎么做。
我尝试创建一个成员 m2 表,但出现错误。我想要实现的是类似于以下伪代码的内容:
SELECT
mn.id,
mn.created_date,
mn.created_by,
mnt.description,
u.handle,
mn.admin_request_company_id,
/*c.company_name*/
FROM member_notification mn,
member_notification_type mnt,
member m,
/*member m2,*/
/*company c,*/
/*user u*/
WHERE mn.member_notification_type_id = mnt.id
AND mnt.name = 'COMPANYVALIDATION'
AND mn.created_by = m.id
AND m.user_id = u.id
/* AND mn.admin_request_company_id = m2.company_id
AND m2.company_id = c.id*/
编辑 表结构如下:
CREATE TABLE member_notification (
id INT NOT NULL AUTO_INCREMENT,
item_id INT NULL,
member_id INT NULL,
text VARCHAR(1) NOT NULL,
member_notification_type_id INT NULL,
marked_read VARCHAR(1) NOT NULL,
created_date DATETIME NOT NULL,
created_by VARCHAR(50) NOT NULL,
admin_request_company_id INT NULL,
PRIMARY KEY (id)
) ENGINE=innodb;
CREATE TABLE member_notification_type (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
description VARCHAR(50) NULL,
PRIMARY KEY (id)
) ENGINE=innodb;
CREATE TABLE member (
id INT NOT NULL AUTO_INCREMENT,
user_id INT NULL,
company_id INT NULL,
soft_delete VARCHAR(10) DEFAULT 'N',
member_status_id INT NOT NULL,
PRIMARY KEY (id)
) ENGINE=innodb;
CREATE TABLE user (
id INT NOT NULL AUTO_INCREMENT,
handle VARCHAR(50) NOT NULL,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
date_of_birth VARCHAR(50) NULL,
soft_delete VARCHAR(10) DEFAULT 'N',
PRIMARY KEY (id)
) ENGINE=innodb;
CREATE TABLE company (
id INT NOT NULL AUTO_INCREMENT,
owning_company_id INT NULL,
company_type_id INT NULL,
name VARCHAR(50) NOT NULL,
company_details_id INT NULL,
PRIMARY KEY (id)
) ENGINE=innodb;