簡體   English   中英

使用內部聯接查詢(SQL)

[英]Query with inner join (sql)

我有3張桌子:

第一個表格是feeds

id
tittle
description
image

第二個表是favorite_feeds

id
feed_id
user_id

第三表是users

id
user_name

我正在嘗試建立多對多關系

例如,如果我想獲得where user_id = 4提要很容易,我可以使用查詢: SELECT feeds INNER JOIN favorite_feeds ON feeds.id = favorite_feeds.id WHERE favorite_feeds.user_id = 4

但可以接收所有擁有和沒有當前用戶的供稿(4)

例如:

id--tittle--description--image--user_id
------------------------------

0--tittle1--description1--image1--4
-----------------------------------
1--tittle2--description1--image1--null
-----------------------------------
2--tittle3--description1--image1--null
-----------------------------------
3--tittle4--description1--image1--4
-----------------------------------

如果Feed中沒有用戶,則user_id = null

我得到的最大信息是這樣的:

id--tittle--description--image--user_id
------------------------------

0--tittle1--description1--image1--4
-----------------------------------
1--tittle2--description2--image1--3
-----------------------------------
1--tittle3--description2--image1--2
-----------------------------------
2--tittle4--description3--image1--4
-----------------------------------
3--tittle2--description4--image1--2
-----------------------------------
3--tittle3--description4--image1--3
-----------------------------------
3--tittle4--description4--image1--4
-----------------------------------

讓我在這里建立一些數據:

表格:

create table users (id int, user_name varchar(20));
insert into users values (1, 'a'), (2, 'b'), (3, 'c'), (4, 'd');

create table feeds (id int, tittle varchar(20), description varchar(20), image varchar(20));
insert into feeds values 
(1, 'title1', 'title1', 'title1'),
(2, 'title2', 'title2', 'title2');

create table favorite_feeds (id int not null auto_increment, feed_id int, user_id int, primary key (id));
insert into favorite_feeds (feed_id, user_id) values 
(1, 1), (1, 2), (1, 4),
(2, 1), (1, 2), (1, 3);

數據

飼料

+------+--------+-------------+--------+
| id   | tittle | description | image  |
+------+--------+-------------+--------+
|    1 | title1 | title1      | title1 |
|    2 | title2 | title2      | title2 |
+------+--------+-------------+--------+

使用者

+------+-----------+
| id   | user_name |
+------+-----------+
|    1 | a         |
|    2 | b         |
|    3 | c         |
|    4 | d         |
+------+-----------+

favorite_feeds

+----+---------+---------+
| id | feed_id | user_id |
+----+---------+---------+
|  1 |       1 |       1 |
|  2 |       1 |       2 |
|  3 |       1 |       4 |
|  4 |       2 |       1 |
|  5 |       1 |       2 |
|  6 |       1 |       3 |
+----+---------+---------+

詢問

select f.*, ff.user_id 
from ( 
  -- find all combinations of feed and userid
  select f.id as fid, u.id as uid 
  from feeds f, users u 
) t 
left join favorite_feeds ff on ff.feed_id = t.fid and ff.user_id = t.uid 
inner join feeds f on f.id = t.fid 
where (ff.user_id is not null and t.uid = 4) 
   or (ff.user_id is null and t.uid = 4);

結果:

+------+--------+-------------+--------+---------+
| id   | tittle | description | image  | user_id |
+------+--------+-------------+--------+---------+
|    1 | title1 | title1      | title1 |       4 |
|    2 | title2 | title2      | title2 |    NULL |
+------+--------+-------------+--------+---------+

替代查詢

select f.*, 4 as user_id
from feeds f 
where exists (
  select 1 from favorite_feeds ff
  where user_id = 4 and feed_id = f.id
)
union all
select f.*, null as user_id
from feeds f 
where NOT exists (
  select 1 from favorite_feeds ff
  where user_id = 4 and feed_id = f.id
)

示例: http//sqlfiddle.com/#!9 / bf490 / 3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM