Jump to content

Counting and sum from different tables


ASK83

Recommended Posts

I'm trying to count and mathematically manipulate two different fields in two different tables as a variable in a query.  Basically it's a user posting system and I'm ordering the query by the most popular submissions using two tables "up" and "down" and setting a variable in the query equal to up minus down.  The query looks like this....

 

$query = "SELECT

news.id AS ni,

COUNT(up.news_id - down.news_id) as c,

user.id,

user.username

  FROM

  news,

user

LEFT JOIN

(up)

ON

(up.news_id = news.id)

LEFT JOIN

(down)

ON

(down.news_id = news.id)

  WHERE

  user.id = news.user_id

  GROUP BY

  news.id

  ORDER BY

  c

DESC

                ";

Link to comment
https://forums.phpfreaks.com/topic/161482-counting-and-sum-from-different-tables/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.