How do I get an idea for each article?

I have 4 tables

articles
category
article_category
view

      

I need to find the number of views for each article Below is my sql query

SELECT a.id,
(SELECT SUM(view) FROM view v ON v.aid=a.id) as view 
FROM articles a JOIN article_category ac ON a.id 
ON ac.aid JOIN category c ON c.id=ac.cid 
LEFT JOIN view cv ON v.aid=a.id GROUP BY a.id

      

My prompt if there are other ways without using an additional SELECT query?

+3


source to share


1 answer


It seems like you just need to take the sum from the subquery and use it.



Select a.ID,SUM(cv.view)
FROM articles a 
JOIN article_category ac ON a.id = ac.aid 
JOIN category c ON c.id=ac.cid 
LEFT JOIN view cv ON v.aid=a.id GROUP BY a.id

      

0


source







All Articles