【问题标题】:php select data from database with joinphp 通过连接从数据库中选择数据
【发布时间】:2017-10-20 18:50:12
【问题描述】:

我是下面的 sql 语句,用于选择用户的笔记和他们关注的用户,如果他们关注另一个用户,则此代码有效:

$sql= "SELECT posts.* FROM  posts 
 JOIN(SELECT * FROM follows WHERE follower = '$get_user')follows
 ON follows.followe = posts.username OR  posts.username = follows.follower  GROUP BY posts.id";

如果他们没有关注任何用户,我会喜欢下面的sql语句的功能。

SELECT * FROM posts where username = '$get_user'

【问题讨论】:

    标签: php mysql select join


    【解决方案1】:

    使用left join 代替join。 左加入将为您提供所有用户,无论他们是否关注用户。如果他们关注了任何用户,你会从第二个表中获取对应的值,如果没有,对应的值(第二列)将包含 null,表示没有一个用户被关注。

    【讨论】:

    • 它不工作,它获取他们没有关注的用户的笔记
    • 这不就是你想要的吗?我通过问题的这些行进行了解释-in case they are not following any user.如果我误解了您的陈述,请纠正我。
    • @Yussuf Hassan 我希望它仅在他们没有关注任何用户的情况下获取他们的笔记
    • 然后使用a left join b on a.condition = b.condition where b.condition is null; 这会给你(a减b),即anot in b中的所有条目。在这种情况下,请记住,您必须在 select 语句中从 table a 中选择条目。
    • 什么是“关注”?
    猜你喜欢
    • 1970-01-01
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    • 2013-10-21
    • 2011-12-08
    • 1970-01-01
    • 2013-08-06
    • 2016-09-11
    相关资源
    最近更新 更多