0
SELECT person.id,
       person.name,
       COUNT(DISTINCT fruit.apple) AS "Red Apple",
       fruit.*
FROM   (SELECT *
        FROM   tree
        ORDER  BY color DESC) AS fruit
       INNER JOIN person
         ON fruit.id = person.id
WHERE  person.name = 'John Smith'  

Now, that code is working before I put in the COUNT(DISTINCT fruit.apple) AS "Apple". what is wrong with that?

Thanks in advance

5
  • i bet the query returns exactly what you have asked it to return. what is your data? what results do you expect? Commented Oct 11, 2011 at 16:31
  • 2
    Define "Not working". Also what is the purpose of ORDER BY color DESC in the derived table definition? Also INNER JOIN people ON fruit.id = person.id definitely won't work. The table name changes midway through. Commented Oct 11, 2011 at 16:31
  • @MartinSmith : Not working like this mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource Commented Oct 11, 2011 at 16:33
  • @JohnSmith - Please post your actual query and explain what it is intended to do. Commented Oct 11, 2011 at 16:38
  • the color actually date type column, so I have to sort it descending, and.. those answers below are true :D thanks guys! Commented Oct 11, 2011 at 16:41

2 Answers 2

4

You don't have a group by clause.

Sign up to request clarification or add additional context in comments.

Comments

0

try this

SELECT person.id,
       person.name,
       COUNT(DISTINCT fruit.apple) AS "Red Apple",
       fruit.*
FROM   (SELECT *
        FROM   tree
        ORDER  BY color DESC) AS fruit
       INNER JOIN people
         ON fruit.id = person.id
WHERE  person.name = 'John Smith' 
GROUP BY fruit.apple

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.