I have the following query against my mysql database.
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;
This is working fine however I now need to extend this to go get a company name off another table. The problem is that to do that I need to re-join against the member table using the key
mn.admin_request_company_id, however I’m not sure how to go about this.
I’ve tried creating a member m2 table but I’m getting errors. What I’m trying to achieve is something along the lines of the following pseudo code:
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*/
edit
Table structures as are follows:
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;
It seems like you just want to use the following which just adds a second join to the
membertable:Notice, I changed your current joins in the
WHEREclause to ANSI join syntax. I also used anINNER JOINbetween the tables, you might need to use aLEFT JOIN.