S.A S.A - 2 months ago 9
MySQL Question

Showing multiple values MySQL

I'm very new to MySQL and need some help with homework, I have researched with www.w3schools.com and tested a number of queries but I can't find the correct method.

The question is "How many houses will have multiple presents delivered?" From this database I have counted that 6 addresses have children receiving more than 1 present but with my query I can't figure out to show just these 6 houses that have 2 or 3 presents.

My query is:

SELECT address, SUM(goodBehaviour) AS `Households with multiple presents delivered`
FROM wishlist
WHERE goodBehaviour >0
GROUP BY address;


My database and output are attached:

MySQL database:


Query output:



What am I missing here?

Answer

Actually your code selects addresses having > 0 but if that is not the cause of your problem then ....

When you say 2 or 3 I assume you mean > 1 also the question title indicates that you want to know how many meet the criteria - I am not being pedantic but just want to give the correct answer :-) The code below should display the addresses that match your selection. If you just want totals you can include the count function. Also attaching files as jpegs is helpful but actual data or sql is better because people can cut and paste to replicate your problem and test instead of having to read it and type it in. Hope this helps.

SELECT *, SUM(goodBehaviour) AS SUM FROM wishlist
 GROUP BY address
having SUM(goodBehaviour) >1

Attach an sql table dump and I will check and provide code for your actual problem

Comments