仅选择特定值

时间:2015-03-24 03:00:04

标签: mysql sql sqlyog

我有2个表,但我只想查询“被拒绝”状态, 表示我需要查询用户仅拒绝状态的结果,而不是批准 & 拒绝,或提交表中的批准

Users Table
-----------
id | name 
-----------
 1 | John
 2 | Doe
 3 | Testing
 4 | Sample



Submission Table
-------------------------------
id | user_id | title  | status
-------------------------------
 1 | 1       | title1 | approved
 2 | 1       | title2 | rejected
 3 | 2       | title3 | approved
 4 | 2       | title4 | approved
 5 | 3       | title5 | rejected
 6 | 3       | title6 | rejected
 7 | 3       | title7 | rejected
 8 | 4       | title8 | approved
 9 | 4       | title9 | approved
 10| 4       | title10| rejected
 11| 4       | title11| rejected

以下是我想要达到的结果:

enter image description here

但我只是通过“拒绝”加入结果查询,但用户仍然有一些“已批准”的结果。

enter image description here

但是通过上述查询,我​​得到了这个结果。

enter image description here

我想查询的是,查询提交的内容只是“拒绝”状态,完全忽略“已批准”,“批准或拒绝”结果。

4 个答案:

答案 0 :(得分:1)

我找到了一个已经使用WHERE NOT EXISTS来过滤提交中的approved结果的解决方案

SELECT u.id AS user_id,s.*, u.name
FROM submissions s
LEFT OUTER JOIN users u
ON s.user_id = u.id

WHERE NOT EXISTS (
    SELECT USER_ID
    FROM submissions tmp
    WHERE tmp.User_ID = s.User_ID
    AND tmp.status = 'approved'
    )

AND STATUS = 'rejected'

答案 1 :(得分:0)

为什么使用左外连接?我相信简单的加入会让你得到结果..

SELECT u.id AS user_id,s.*, u.name
FROM submissions s
JOIN users u
ON s.user_id = u.id AND s.status = 'rejected'

OR

SELECT u.id AS user_id,s.*, u.name
FROM submissions s
JOIN users u
ON s.user_id = u.id 
WHERE s.status = 'rejected'

答案 2 :(得分:0)

create table users
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,name VARCHAR(12) NOT NULL UNIQUE
);

INSERT INTO users VALUES
(1,'John'),
(2,'Doe'),
(3,'Testing'),
(4,'Sample');

create table submissions
(id int not null auto_increment primary key
,userid int not null
,title varchar(12) not null
,status varchar(12) not null
);

insert into submissions values
( 1,1,'title1','approved'),
( 2,1,'title2','rejected'),
( 3,2,'title3','approved'),
( 4,2,'title4','approved'),
( 5,3,'title5','rejected'),
( 6,3,'title6','rejected'),
( 7,3,'title7','rejected'),
( 8,4,'title8','approved'),
( 9,4,'title9','approved'),
(10,4,'title10','rejected'),
(11,4,'title11','rejected');

SELECT u.name
     , x.* 
  FROM submissions x
  JOIN users u
    ON u.id = x.userid
  LEFT
  JOIN submissions y
    ON y.status = 'approved'
   AND y.userid = x.userid
 WHERE x.status = 'rejected'
   AND y.userid IS NULL;
+---------+----+--------+--------+----------+
| name    | id | userid | title  | status   |
+---------+----+--------+--------+----------+
| Testing |  5 |      3 | title5 | rejected |
| Testing |  6 |      3 | title6 | rejected |
| Testing |  7 |      3 | title7 | rejected |
+---------+----+--------+--------+----------+

http://sqlfiddle.com/#!9/1e9da/1

答案 3 :(得分:-1)

您可以使用以下NOT IN(your exclude criteria)格式。

SELECT u.id AS user_id,s.*, u.name
FROM submissions s
LEFT OUTER JOIN users u
ON s.user_id = u.id

WHERE s.user_id NOT IN(
SELECT tmp.user_id
FROM submissions tmp
WHERE tmp.status = 'approved'
)

AND STATUS = 'rejected'