0

通过选择一列中具有唯一 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
4

5 回答 5

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
于 2013-03-29T07:27:25.333 回答
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     |
 +------------+---------+------------+---------+---------+-----------+------------+------------+--------------+
于 2013-03-29T07:31:11.127 回答
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'

希望这对你有帮助

于 2013-03-29T07:12:27.193 回答
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';
于 2013-03-29T07:19:13.527 回答
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
于 2013-03-29T07:25:08.900 回答