mysql表连接:多个连接到同一个表但具有不同的参数

时间:2013-01-22 10:53:39

标签: mysql sql

我对我的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;

3 个答案:

答案 0 :(得分:2)

您似乎只想使用以下内容,只需在member表中添加第二个连接:

SELECT
  mn.id,
  mn.created_date,
  mn.created_by,
  mnt.description,
  u.handle,
  mn.admin_request_company_id,
  c.name
FROM member_notification mn
INNER JOIN member_notification_type mnt
  on mn.member_notification_type_id = mnt.id
INNER JOIN member m1
  on mn.created_by = m1.id
INNER JOIN user u
  on m1.user_id = u.id
INNER JOIN member m2
  on mn.admin_request_company_id = m2.company_id
INNER JOIN company c
  on m2.company_id = c.id
WHERE mnt.name = 'COMPANYVALIDATION'

注意,我将WHERE子句中的当前连接更改为ANSI连接语法。我还在表之间使用了INNER JOIN,您可能需要使用LEFT JOIN

答案 1 :(得分:1)

要在表格上连接两次,请使用其他名称选择表格两次:

SELECT
  ...
FROM 
  member m1,
  member m2,
  someCouplingTable t
WHERE
  m1.Id = t.leftId
  AND m2.Id = t.rightId

答案 2 :(得分:1)

以下是如何做到这一点

SELECT
  mn.id,
  mn.created_date,
  mn.created_by,
  mnt.description,
  u.handle,
  mn.admin_request_company_id
FROM member_notification mn
  left join member_notification_type mnt
    on mnt.id = mn.member_notification_type_id
  left join member m
    on m.id = mn.created_by
  left join user u
    on u.id = m.user_id
  left join company as c
    on c.id = m.company_id
where mnt.name = 'COMPANYVALIDATION'