Welcome Guest, Not a member yet? Register   Sign In
mySQL query help
#1

[eluser]new_igniter[/eluser]
Hello,
I am having tons of difficulty with this query. I am not too familiar with mySQL queries, so I don't understand what to change. Any help would be more appreciated.

* I have 3 tables, twibsDB, twibsReviews, twibsFeaturedVoting, each related by twibsID
o twibsDB - main table, bios, pic, username, etc
o twibsReviews - reviews relating to entities in twibsDB
o twibsFeaturedVoting - votes relating to entities in twibsDB
* I get 50 rows correctly back when I run the query, but the value for numRev is never right. It seems to be always the sum of ALL votes and not just the count relative to twibsID.
* I am trying to get the total number of votes for each entity for numRev. So if cnnbrk got 3 votes, numRev woudl be 3.

Code:
SELECT twibsID, twibAlias, twibName, twibBio, twibPic, twibLocation, twibURL, twibEmail, twibBlog, twibStore, twibPromotion, followersCount, created_at, count( tRating ) AS numRev, sum( vote ) AS vote
FROM twibsDB
JOIN twibsFeaturedVoting
USING ( twibsID )
LEFT JOIN twibsReviews
USING ( twibsID )
WHERE ( twibsFeaturedVoting.twibsID !=0 AND domainSource = '0')
AND (
twibsFeaturedVoting.createTime >= '2009-04-22'
AND twibsFeaturedVoting.createTime <= '2009-05-08'
)

GROUP BY `twibsID`
ORDER BY `vote` DESC
LIMIT 50
#2

[eluser]dmorin[/eluser]
It's probably going to be easiest for you to use separate queries rather than trying to do everything in one. The problem is that you're doing multiple joins combined with aggregate functions which will almost certainly through off your aggregated counts and sums.

Do the first query to get all of the rows you want from twibsDB, make an array of all the ids you want, and then use where_in() to query each other table, grouping on the twibsID column.




Theme © iAndrew 2016 - Forum software by © MyBB