drk49438 2016-12-19 11:42
浏览 83
已采纳

使用join从两个以上的表中进行选择会选择重复项

I have 3 table on my mysql db (user, follower, post), I wrote an sql, that get all posts from the people a user followed.

sql

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

result

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

user table

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

follower table

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

post table

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

so now am trying to select post of people the user is following and the posts of the user

I tried this 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"

result

null

Please is what am trying to achieve with the sql possible, if(possible) {"what_am_i_doing_wrong"}();

edits

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
  • 写回答

2条回答 默认 最新

  • dongshimao7115 2016-12-19 11:54
    关注
    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟