SQL查询在表的列中选择具有唯一记录的行?

时间:2013-03-29 07:09:51

标签: mysql sql

通过在一列中选择具有唯一ID的行,我的查询出现问题。

这是我的数据库看起来像:

Table 1

user_id  | user_name | user_email
1          john         ex0@email
2          nathel       ex1@email
3          bob          ex2@email

Table 2

subs_id | user_id | prod_id
1         1           1
2         1           2
3         2           1
4         3           1
5         3           3


Table 3

prod_id | prod_name 
1         Platinum
2         Gold
3         Steel

我需要做的是从表1中选择用户行加入表2& 3根据其订阅在表2 user_id列中具有唯一记录。

实施例。我想选择仅以铂金订阅的用户,因此输出必须如下所示:

user_id | user_name | user_email  | subs_id | user_id | prod_id | prod_id |  prod_name 
2          nathel      ex1@email     3         2           1       1         Platinum

5 个答案:

答案 0 :(得分:1)

SELECT *  from users u inner join subscription s on u.user_id = s.user_id
inner join products p on p.prod_id = s.prod_id 
 WHERE s.user_id IN (SELECT user_id from subscription GROUP by user_id HAVING count(user_id) = 1) AND s.prod_id = 1

答案 1 :(得分:1)

最后一块拼图被省略作为读者的练习......

 DROP TABLE IF EXISTS users;

 CREATE TABLE users
 (user_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
 ,user_name VARCHAR(20) NOT NULL UNIQUE
 ,user_email VARCHAR(12) NOT NULL
 );

 INSERT INTO users VALUES
 (1 ,'john','ex0@email'),
 (2 ,'nathel','ex1@email'),
 (3 ,'bob','ex2@email');

 DROP TABLE IF EXISTS product_user;
 CREATE TABLE product_user 
 (product_id INT NOT NULL
 ,user_id INT NOT NULL
 ,PRIMARY KEY (product_id,user_id)
 );

 INSERT INTO product_user VALUES
 (1,  1),(2  ,1),(1  ,2),(1,3),(3,3);

 DROP TABLE IF EXISTS products;

 CREATE TABLE products
 (product_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,product_name VARCHAR(12) NOT NULL UNIQUE);

 INSERT INTO products VALUES (1 ,'Platinum'),(2,'Gold'),(3,'Steel');

 SELECT * 
   FROM product_user x 
   LEFT 
   JOIN product_user y 
     ON y.user_id = x.user_id 
    AND y.product_id <> x.product_id 
  WHERE x.product_id =1;
 +------------+---------+------------+---------+
 | product_id | user_id | product_id | user_id |
 +------------+---------+------------+---------+
 |          1 |       1 |          2 |       1 |
 |          1 |       2 |       NULL |    NULL |
 |          1 |       3 |          3 |       3 |
 +------------+---------+------------+---------+

 SELECT * 
   FROM product_user x 
   LEFT 
   JOIN product_user y 
     ON y.user_id = x.user_id 
    AND y.product_id <> x.product_id 
   JOIN users u 
     ON u.user_id = x.user_id 
   JOIN products p 
     ON p.product_id = x.product_id 
  WHERE p.product_name = 'Platinum';
 +------------+---------+------------+---------+---------+-----------+------------+------------+--------------+
 | product_id | user_id | product_id | user_id | user_id | user_name | user_email | product_id | product_name |
 +------------+---------+------------+---------+---------+-----------+------------+------------+--------------+
 |          1 |       1 |          2 |       1 |       1 | john      | ex0@email  |          1 | Platinum     |
 |          1 |       2 |       NULL |    NULL |       2 | nathel    | ex1@email  |          1 | Platinum     |
 |          1 |       3 |          3 |       3 |       3 | bob       | ex2@email  |          1 | Platinum     |
 +------------+---------+------------+---------+---------+-----------+------------+------------+--------------+

答案 2 :(得分:0)

我认为您可以使用DISTINCT从查询中的数据库中获取唯一记录

SELECT DISTINCT 
    t1.`user_id` ,t1.`user_name`,t1.`user_email`,t2.`subs_id`,t2.`user_id`,t2.`prod_id`,t3.`prod_id` ,t3.`prod_name`
FROM Table1 t1 
     INNER JOIN Table2 t2 
        ON t1.`user_id`=t2.`user_id` 
     INNER JOIN Table3 t3 
        ON t2.`prod_id`=t3.`prod_id` 
WHERE 
     t1.`user_id`='2'

希望这会对你有所帮助

答案 3 :(得分:0)

希望所以你正在寻找这个:

SELECT * FROM table1 as t1 
    inner join table2 as t2 on t1.user_id = t2.user_id 
    inner join table3 as t3 on t2.prod_id = t3.prod_id 
    where t3.prod_name = 'Platinum';

答案 4 :(得分:0)

请!尝试下面的代码片段

select t1.user_id ,t1.user_name,t1.user_email,
t2.subs_id,t2.user_id , t2.prod_id , 
t3.prod_id , t3.prod_name
from table2 t2 inner join table1 t1 on t2.user_id=t1.user_id
inner join table3 t3 on t3.prod_id=t2.prod_id and t2.prod_id =1