I've recently taken over a site with some SQL queries that seem to be a bit beyond me. What the query has to do is find a Trade that belongs to a User, then find the Feedback score (1,2, or 3) attached to that trade. It's supposed to count all the ratings (1 being negative, 2 neutral, 3 positive) for me to use later. Here's the code:
$user = mysql_fetch_assoc($this->db_query('
SELECT
`UserAccess`.*,
COUNT(IF(`Feedback`.`rating`=1, 1, NULL)) AS "negative",
COUNT(IF(`Feedback`.`rating`=2, 1, NULL)) AS "neutral",
COUNT(IF(`Feedback`.`rating`=3, 1, NULL)) AS "positive"
FROM
`UserAccess`
LEFT JOIN
`Trades`
ON
`Trades`.`owner` = `UserAccess`.`ID`
LEFT JOIN
`Feedback`
ON
`Feedback`.`trade` = `Trades`.`ID`
WHERE
`UserAccess`.`ID` = "'.$_COOKIE[Auth::$config['cookie_name_id']].'"
GROUP BY
`UserAccess`.`ID`
LIMIT
1
'));
The idea being I can use these numbers to calculate an overall rating, using user['negative']
, user['positive']
etc.
This involves a lot of new concepts for me with sql, so I'm having trouble pinning down the fault. It may also be relevant that I think the old developer was using an earlier version of PHP to me (so it may have been working properly for him).
Thanks
$user['positive'] = 2;
the rest of my code works fine, so it's definitely part of the query. As far as I can see, all the table and column names are correct, too. Argh! – themartin Dec 06 '13 at 07:02