views:

373

answers:

5

Just got this answer from a previous question and it works a treat!

SELECT username, (SUM(rating)/COUNT(*)) as TheAverage, Count(*) as TheCount 
FROM ratings WHERE month='Aug' GROUP BY username HAVING TheCount > 4
ORDER BY TheAverage DESC, TheCount DESC

But when I stick this extra bit in it gives this error:

Documentation #1267 - Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (latin1_general_ci,IMPLICIT) for operation '='

SELECT username, (SUM(rating)/COUNT(*)) as TheAverage, Count(*) as TheCount FROM 
ratings WHERE month='Aug' 
**AND username IN (SELECT username FROM users WHERE gender =1)**
GROUP BY username HAVING TheCount > 4 ORDER BY TheAverage DESC, TheCount DESC

The table is:

id, username, rating, month

Cheers

A: 

Make sure your version of MySQL supports subqueries (4.1+). Next, you could try rewriting your query to something like this:

SELECT ratings.username, (SUM(rating)/COUNT(*)) as TheAverage, Count(*) as TheCount FROM ratings, users 
WHERE ratings.month='Aug' and ratings.username = users.username
AND users.gender = 1
GROUP BY ratings.username
HAVING TheCount > 4 ORDER BY TheAverage DESC, TheCount DESC
stereointeractive.com
by the way, is your users.gender column an int type?
stereointeractive.com
I tried that:SELECT ratings.username, (SUM(ratings.rating)/COUNT(*)) as TheAverage, Count(*) as TheCount FROM ratings, users WHERE ratings.month='Aug' and ratings.username = users.usernameAND users.gender = 1 GROUP BY ratings.username HAVING TheCount > 4 ORDER BY TheAverage DESC, TheCount DESCGot the same error as before? Yes, Gender is an int.
Oliver
A: 
HAvING TheCount > 4 AND username IN (SELECT username FROM users WHERE gender=1)

but why i am answering, you dont voted me as right answer :)

Rufinus
Hi I already tried it that way and got the same error!
Oliver
ok i see know the error you postet in the comment. does the error not says it all ? your tables or fields have an mixed collation. changed it so all tables/fields have the same collation.
Rufinus
btw. you really should use a join as suggested by stereointeractive.com your subquery gets executed for every row which is kinda slow. see also http://dev.mysql.com/doc/refman/5.0/en/optimizing-subqueries.html
Rufinus
A: 
  • Check that your users.gender column is an INTEGER.
  • Try: alter table users convert to character set latin1 collate latin1_swedish_ci;
hobodave
A: 
SELECT  username, AVG(rating) as TheAverage, COUNT(*) as TheCount
FROM    ratings
        WHERE month='Aug'
        AND username COLLATE latin1_general_ci IN
        (
        SELECT  username
        FROM    users
        WHERE   gender = 1
        )
GROUP BY
        username
HAVING
        TheCount > 4
ORDER BY
        TheAverage DESC, TheCount DESC;
Quassnoi
A: 

1267 - Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and

(latin1_general_ci,IMPLICIT) for operation '='

he table is:

id, username, rating, month

Check the collation type of each table, and make sure that they have the same collation.

After that check also the collation type of each table field that you have use in operation.

I had encountered the same error, and that tricks works on me.