[英]Selecting using SQL Subqueries or the JOIN Clause
So I have these tables. 所以我有这些表。
This table stores likes: 该表存储点赞:
Table Name: Action
Id Creator Type Target <--- The `Target` here is the `Id` in Posts
1 1 like 1
2 2 like 1
3 3 like 1
4 1 like 2
This table stores posts: 该表存储帖子:
Id Creator
1 1
2 2
The column name Creator
in any table refers to the Id
column in the Account
table (not shown here). 任何表中的列名
Creator
都是指Account
表中的Id
列(此处未显示)。
I would like to select the total number of likes that have been given to an account's posts. 我想选择一个帐户帖子获得的喜欢总数。 I have tried the query below but it gave a syntax error.
我已经尝试过下面的查询,但是它给出了语法错误。
SELECT count(`Id`) AS Check FROM `Action` WHERE `Type`='like' AND `Target`= (SELECT `Id` FROM `Posts` WHERE `Creator` = '1');
I'm not sure if a JOIN clause would be more appropriate here. 我不确定在这里JOIN子句是否更合适。
You will get the sums with a join of both tables and GROUP BY
and counting the appropriate targets. 通过将表和
GROUP BY
结合在一起并计算适当的目标,您将获得总和。
SELECT
p.creator,
COUNT(a.target)
FROM
posts p
INNER JOIN
Action a
ON
a.target = p.id
WHERE
a.Type = 'like' AND p.creator = 1
GROUP BY
p.creator;
Here is the SQL 这是SQL
select a.ID,a.Creator,a.Type,a.Target, Count(p.*)Total
from Action a
join posts p on a.Target=p.Creator
where
a.Type='like'
and a.Target=1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.