SQL count()

So how do yo find the most popular cake in the Shire Bakery? Here’s an idea.

PhpMyAdmin: the most popular caske
PhpMyAdmin: the most popular caske
-- Show the best cookie
-- See: Ben Forta Chap. 16
SELECT COUNT( cakes.cake ) AS 'popular', cakes.cake, costumers.who
FROM cakes, costumers, likes
-- sort out the cardinalities (relations)
WHERE cakes.cakes_id = likes.cakes_id
AND costumers.costumers_id = likes.costumers_id

-- group by the cake 
group by cakes.cake

-- and sort (most popular and downwards)
order by popular desc

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Enable Notifications OK No thanks

We use cookies - more information

Multimusen.dk will set a few cookies from Doubleclick, Google and the Social Media plugins they ay set some cookies. Some of my pages use APIs - such as YouTube, LinkedIn, Google Fonts, Google Maps, Mapbox, Spotify, Jetpack, Twitter, Facebook &c.. Such plugins may set the odd cookie.

Close