SELECT查询三个连接的表

时间:2015-07-22 13:52:36

标签: mysql database select

我的表格结构如下所示:

 subversions(
                id INT(10) AUTO_INCREMENT,
                version_id INT(10)
                name VARCHAR(16) NOT NULL, 
                PRIMARY KEY (id),
                FOREIGN KEY (version_id) REFERENCES versions(id)
            );
/*      ^
    Many|
       To
        | Many
        v
*/
users(
    id INT(10) AUTO_INCREMENT,
    name VARCHAR(16) NOT NULL, 
    password VARCHAR(32) NOT NULL,
    PRIMARY KEY (id),
);

带联结表

subversions_users_conjuction(
    id INT(10) AUTO_INCREMENT,
    subversion_id VARCHAR(16) NOT NULL, 
    user_id VARCHAR(32) NOT NULL,
    PRIMARY KEY (id),
    FOREIGN KEY (subversion_id) REFERENCES subversions(id),
    FOREIGN KEY (user_id) REFERENCES users(id)    
);

我的问题是设计查询以选择“用户”中的所有用户不在 subversions_users_conjuction 与subversion_id = specificID 并且没有得到那些

如下所示:

//Query for all other users
$res2 = mysqli_query($conn,"SELECT DISTINCT i.id, i.username FROM users AS i
                    NOT IN 
                    SELECT DISTINCT u.id, u.username FROM users AS u
                    INNER JOIN subversion_user ON u.id = subversion_user.user_id
                    WHERE subversion_user.subversion_id = '$subversionID'");

但是,这个不起作用 SQL错误说:

MySQL Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NOT IN 
                        SELECT DISTINCT u.id, u.username FROM users AS u
                        INNER JOI' at line 2

2 个答案:

答案 0 :(得分:0)

您有三个表,并且想要加入它们。此外,您希望找到缺少的条目'来自正常的内部联接。在这种情况下,您需要外部联接。 (不幸的是mySQL并没有包括一个完整的外部加入)

My SQLfiddle

我的测试代码:

CREATE TABLE subversion
(
   id INT(10) AUTO_INCREMENT,
   name VARCHAR(16) NOT NULL, 
   PRIMARY KEY (id)
);

INSERT INTO subversion (name) VALUES ('Terror');
INSERT INTO subversion (name) VALUES ('Mayhem');
INSERT INTO subversion (name) VALUES ('Bribery');
INSERT INTO subversion (name) VALUES ('Eating Ice Cream');
INSERT INTO subversion (name) VALUES ('Unrest');
INSERT INTO subversion (name) VALUES ('Infiltration');

CREATE TABLE users(
    id INT(10) AUTO_INCREMENT,
    name VARCHAR(16) NOT NULL, 
    password VARCHAR(32) NOT NULL,
    PRIMARY KEY (id)
);

INSERT INTO users (name, password) VALUES
('Adam', '12345'),('Bill', '78903'), ('Chandra', '56565'),
('David', '34543'), ('Edwina', 'abcde'), ('Faheem', 'jklab'), ('George', 'vfghn'), ('Hank', '2ws3e') ;

CREATE TABLE subversions_users_conjuction(
    id INT(10) AUTO_INCREMENT,
    subversion_id INT(10) NOT NULL, 
    user_id INT(10) NOT NULL,
    PRIMARY KEY (id),
    FOREIGN KEY (subversion_id) REFERENCES subversion(id),
    FOREIGN KEY (user_id) REFERENCES users(id)    
);

INSERT INTO subversions_users_conjuction (user_id, subversion_id) VALUES
(1,1), (2,2), (3,1), (4,4), (5,5);

我的LEFT OUTER查询

SELECT * FROM 
users LEFT OUTER JOIN subversions_users_conjuction 
ON users.id = subversions_users_conjuction.user_id
LEFT OUTER JOIN subversion ON subversion.id = subversions_users_conjuction.subversion_id 
WHERE subversion_id IS NULL;

结果:

| id |   name | password |     id | subversion_id | user_id |     id |   name |
|----|--------|----------|--------|---------------|---------|--------|--------|
|  6 | Faheem |    jklab | (null) |        (null) |  (null) | (null) | (null) |
|  7 | George |    vfghn | (null) |        (null) |  (null) | (null) | (null) |
|  8 |   Hank |    2ws3e | (null) |        (null) |  (null) | (null) | (null) |

我在这里学到的东西:

答案 1 :(得分:0)

我以此查询的形式找到了解决问题的方法:

//Query for all other users
    $res2 = mysqli_query($conn,"SELECT DISTINCT id, username FROM users 
                         WHERE id  NOT IN ( 
                                SELECT DISTINCT u.id  FROM users AS u
                                INNER JOIN subversion_user ON u.id = subversion_user.user_id
                                WHERE subversion_user.subversion_id = '$subversionID' 
                            )");