gpt4 book ai didi

php - 使用连接从两个以上的表中选择重复项

转载 作者:搜寻专家 更新时间:2023-10-30 22:08:32 26 4
gpt4 key购买 nike

我的 mysql 数据库上有 3 个表(用户、关注者、帖子),我写了一个 sql,它从用户关注的人那里获取所有帖子。

sql

SELECT post.* FROM post JOIN
follower ON post.owner_user_id = follower.user_id AND
follower.follower_id = "3"

结果

id | owner_user_id | content
2 | 1 | why are all my senior developers jerks?
3 | 1 | PHP7 in your face node.js

用户表

id | username | password
1 | user1 | 12345678
2 | user2 | 12345678
3 | user3 | 12345678

跟随表

user_id | follower_id
3 | 1
3 | 2
1 | 3

张贴表格

id | owner_user_id | content
1 | 2 | reading a 1k+ page on mysql, do i need to?
2 | 1 | why are all my senior developers jerks?
3 | 1 | PHP7!, in your face node.js
3 | 3 | I posted

所以现在我正在尝试选择用户关注的人的帖子和用户的帖子

我试过这个sql

sql

SELECT post.* FROM post JOIN
follower ON post.owner_user_id = follower.user_id AND
follower.follower_id = "3" JOIN
user ON post.owner_user_id = user.id= "3"

结果

null

请问我试图用 sql 实现什么,如果(可能){“what_am_i_doing_wrong”}();

编辑

user_id 3 has a post, still running the above sql returns null, was hoping if the user had no post, only post of the people the user is following is returned

最佳答案

select post.* from 
(select user_id from follower where follower_id = 3 union select 3) blah
join post on blah.user_id = post.owner_user_id;

关于php - 使用连接从两个以上的表中选择重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41221839/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com