我對我的mysql數據庫有以下查詢。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;
什麼是你的表結構 –
嗯大膽似乎並沒有工作,但增加的部分是**評論 – sapatos
你能提供你的錯誤之間? –