具有内部联接的单个SQL查询

时间:2015-04-27 21:01:28

标签: sql

好吧基本上我有2个表,一个是用户,另一个是跟踪日志,我有一个PHP脚本,我需要发送请求以找到要关注的用户。

好吧,假设我在用户'gretar'(promote_account)上,我需要找到要关注的用户,我需要选择所有拥有promotion_left>的用户0并且不在follow_account WHERE twitter_name是'gretar'。

users_table: enter image description here

follow_table: enter image description here

编辑:阅读所有内容,我显然没有解释得很清楚,对不起,我现在已经改变了。

1 个答案:

答案 0 :(得分:0)

您希望左外部联接显示下表中没有记录的用户。所以在TSQL中,它看起来像这样:

SELECT *
  FROM users_table u
  LEFT OUTER JOIN follow_table f ON f.followed_account = u.promote_account
    AND twitter_name = 'gretar'
  WHERE f.id IS NULL
    AND u.promotion_left > 0
    AND u.promote_account <> 'gretar'
相关问题