php - Mysql select sum() from another table -


i have tables :

table: articles

   id   |   title   |   display   |  -----------------------------------     1   |   fkekc   |      1      |     2   |   ldsdf   |      1      |     3   |   otrld   |      0      |     4   |   qcrsa   |      1      | 

table: likes

   id  |  article_id |     |  type  ----------------------------------------     1   |      1     |   121    |   1     2   |      1     |   652    |   2     3   |      2     |   12     |   1     4   |      1     |   5      |   3 

i want result:

article [1] => 778 article [2] => 12 article [3] => 0 article [4] => 0 

i use left join between 2 tables return records per likes table. 3 record of article 1

my code:

 select articles.*,likes.like `articles` left join `likes` on articles.id=likes.article_id display='1' 

i know must use sum() didn't know how use it

with answers find must use this:

select articles.*, sum(likes.like) likessum `articles` left join `likes`on articles.id=likes.article_id display='1' group articles.id 

but want set filter in query. use :

select articles.*, sum(likes.like) likessum `articles` left join `likes`on articles.id=likes.article_id display='1' && likessum>='100' group articles.id 

but above code doesn't return result

this query

select articles.*,coalesce(sum(likes.like),0) total_like   `articles` left join `likes` on articles.id=likes.article_id group    articles.id 

output enter image description here


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -