我有3张桌子:
第一个表是feeds
:
id
tittle
description
image
第二个表是favorite_feeds
:
id
feed_id
user_id
第三个表是users
:
id
user_name
我试图建立多对多的关系
例如,如果我想要使用where user_id = 4
获取Feed很简单,我会使用查询:SELECT feeds INNER JOIN favorite_feeds ON feeds.id = favorite_feeds.id WHERE favorite_feeds.user_id = 4
但是可以接收所有拥有并且没有当前用户的Feed(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
-----------------------------------
答案 0 :(得分:0)
让我在这里建立一些数据:
<强>表格强>
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
)