简体   繁体   English

如何从两个不同的表中进行选择和计数,以在一个查询中获得帖子的“喜欢计数”?

[英]How do I SELECT and COUNT from two diferent tables to get the “like count” of a post in one query?

I'm kind of noobie to this, but I'm trying to learn, I have two tables, the first one (NEWS) has all the information about posts of a blog, it has the follow structure: 我对此有点怀疑,但是我想学习,我有两个表,第一个表(NEWS)具有有关博客帖子的所有信息,它具有以下结构:

* NEWS (TABLE 1)
- id_new
- id_category
- date
- ...etc
- **likes**

and I have a second table: 我还有第二张桌子:

* LIKES (TABLE 2)
- id_like
- id_new
- id_user
- date
- ip_user

So, I want to select all the rows from TABLE 1 to display all the news but also i want to count the likes and get the COUNT of each new as like column. 所以,我想选择所有从表1的行来显示所有的消息,但我也想算喜欢,让每一个新的COUNT 列。

You can do it like this 你可以这样

SELECT table1.*, table2.*, count(table2.id_like) as like FROM news AS table1 
INNER JOIN likes AS table 2 ON table1.id_new = table2.id_new;

OR 要么

SELECT table1.*, table2.*, count(table2.id_like) as like FROM news AS table1 
LEFT JOIN likes AS table 2 ON table1.id_new = table2.id_new;

you can use prepared statement 您可以使用准备好的语句

for example 例如

$stmt = $pdo->prepare("SELECT count(*) FROM TABLE_1 ); $ stmt = $ pdo-> prepare(“ SELECT count(*)FROM TABLE_1 );

$stmt2 = $pdo->prepare("SELECT count(*) FROM TABLE_2 ); $ stmt2 = $ pdo-> prepare(“ SELECT count(*)FROM TABLE_2 );

//then execute //然后执行

just read more on prepared statement 只需阅读更多有关准备好的声明

尝试这个

SELECT n, (SELECT count(*) FROM like l WHERE l.id_new = n.id_new) FROM news n

使用类似:

SELECT *, (SELECT COUNT(*) FROM LIKES WHERE LIKES.id_new =id_new) AS newsLikesCount FROM NEWS ORDER BY date;

This approach left joins the NEWS table to a subquery which finds the number of likes for each news story. 剩下的方法是将NEWS表连接到一个子查询,该子查询查找每个新闻故事的点赞次数。

SELECT
    t1.*,
    COALESCE(t2.likes, 0) AS likes
FROM NEWS t1
LEFT JOIN
(
    SELECT id_new, COUNT(*) AS likes
    FROM LIKES
    GROUP BY id_new
) t2
    ON t1.id_new = t2.id_new

Note that here a story having no likes would not appear at all in the LIKES table and would receive a count of zero. 请注意,这里没有喜欢的故事根本不会出现在LIKES表中,并且计数为零。 Also note that I assume that every record in the LIKES table corresponds to a logical like. 还要注意,我假设LIKES表中的每个记录都对应一个逻辑类似。 If not, then the query could be modified to count something else. 如果不是,则可以修改查询以计算其他内容。

This query would return all news and their number of likes 该查询将返回所有新闻及其喜欢的次数

select n1.* , numberOfLikes.number_of_likes
from news n1 
left join 
(select n.id_news, count(l.id_like) as number_of_likes
from news n 
left join likes l on n.id_news = l.id_new
group by n.id_news) numberOfLikes on n1.id_news = numberOfLikes.id_news

The important concepts here is understanding how two tables are joined together (1), how group by works(2), and how to aggregate l.id_likes using count (3). 这里的重要概念是了解如何将两个表连接在一起(1),如何group by工作原理group by (2)以及如何使用count (3)汇总l.id_likes

(1). (1)。 Left join preserves everything in the NEWS table and join them with news link to the news. 左联接将保留NEWS表中的所有内容,并通过新闻的新闻链接将它们联接在一起。

(2). (2)。 Then we group the rows base on id_news from the news. 然后,我们根据新闻中的id_news对行进行分组。 However, mysql gets confused because it doesn't know what to do with id_like from the likes table that we included in our select clause. 但是,mysql感到困惑,因为它不知道如何处理我们包含在select子句中的likes表中的id_like。 Don't worry my friend, This is where count comes in. 别担心,我的朋友,这就是计数的来源。

(3). (3)。 We count the number of id_likes base for each id_news since we are grouping the rows base on id_news. 因为我们基于id_news对行进行分组,所以我们计算每个id_news的id_likes基数。

I hope this helps. 我希望这有帮助。 and welcome to StackOverfow. 欢迎来到StackOverfow。 If you find this answer of any other answer helpful please mark it as the solution. 如果您发现此答案对其他答案有帮助,请将其标记为解决方案。 That way it will help the community and fellow programmers in the future if they run into the same problem as you. 这样,如果社区和其他程序员遇到与您相同的问题,它将在将来对他们有所帮助。 Cheers. 干杯。

Edit: to include all columns from news table we simply join the result from above back to the news table itself. 编辑:要包括新闻表中的所有列,我们只需将结果从上方连接回新闻表本身即可。 and we select everything from the news table n1 and only number_of_likes from the result we created above. 然后从新闻表n1中选择所有内容,从上面创建的结果中仅选择number_of_likes。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM