Reputation: 149
I've gotten my database set up for my website and I'm a little baffled on how I should write the SQL query to display the information how I want it.
Here's an image of how my database is setup and an example of how I want to display it:
Here's the SQL query I wrote to get the information (Im open to writing a better solution)
SELECT * FROM drinks_category, drinks_lookup, drinks
WHERE drinks.drink_id = drinks_lookup.drink_id
AND drinks_lookup.drinks_category_id = drinks_category.drinks_category_id
This gets me all the info but I'm not sure how to write the output to get it to show how i want.
Upvotes: 0
Views: 128
Reputation: 21513
Hideously over complicated way to do it in a single sql statement for 3 fixed columns, coping with up to 1000 drinks in each category.
SELECT DrinkCategory1Subselect.drink_name AS 'Drink Category Title', DrinkCategory2Subselect.drink_name AS 'Drink Category Title 2', DrinkCategory3Subselect.drink_name AS 'Drink Category Title 3'
FROM (SELECT a.i+b.i*10+c.1*100 AS aCounter
FROM (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) a,
(SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) b,
(SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) c) Deriv1
INNER JOIN (SELECT c.drink_name,
@Counter := @Counter + 1 AS aCounter
FROM drinks_category a
INNER JOIN drinks_lookup b ON a.drink_category_id = b.drink_category_id
INNER JOIN drinks c ON b.drink_id = c.drink_id
CROSS JOIN (SELECT @Counter:=0) NothingUseful
WHERE a.drink_category_id = 1
ORDER BY c.drink_id) DrinkCategory1Subselect
ON DrinkCategory1Subselect.aCounter = Deriv1.aCounter
INNER JOIN (SELECT c.drink_name,
@Counter := @Counter + 1 AS aCounter
FROM drinks_category a
INNER JOIN drinks_lookup b ON a.drink_category_id = b.drink_category_id
INNER JOIN drinks c ON b.drink_id = c.drink_id
CROSS JOIN (SELECT @Counter:=0) NothingUseful
WHERE a.drink_category_id = 2
ORDER BY c.drink_id) DrinkCategory2Subselect
ON DrinkCategory2Subselect.aCounter = Deriv1.aCounter
INNER JOIN (SELECT c.drink_name,
@Counter := @Counter + 1 AS aCounter
FROM drinks_category a
INNER JOIN drinks_lookup b ON a.drink_category_id = b.drink_category_id
INNER JOIN drinks c ON b.drink_id = c.drink_id
CROSS JOIN (SELECT @Counter:=0) NothingUseful
WHERE a.drink_category_id = 3
ORDER BY c.drink_id) DrinkCategory3Subselect
ON DrinkCategory3Subselect.aCounter = Deriv1.aCounter
WHERE DrinkCategory1Subselect.drink_name IS NOT NULL
OR DrinkCategory2Subselect.drink_name IS NOT NULL
OR DrinkCategory3Subselect.drink_name IS NOT NULL
ORDER BY Deriv1.aCounter
Not tested. Better to do the formatting in php, but was bored
Upvotes: 0
Reputation: 71918
One solution is to order by category:
SELECT *
FROM drinks_category, drinks_lookup, drinks
WHERE drinks.drink_id = drinks_lookup.drink_id
AND drinks_lookup.drinks_category_id = drinks_category.drinks_category_id
ORDER BY drinks_category.drink_category_title
With that, you can loop in PHP, and output a new header whenever a new category appears. Something like this:
<?php
$last_category = 0;
foreach($data as $row) {
if($row['drinks_category_id'] != $last_category) {
echo '<h1>' . $row['drink_category_title'] . '</h1>';
}
echo '<div>' . $row['drinks_name'] . '</div>';
$last_category = $row['drinks_category_id'];
}
?>
You should probably use nested html lists instead of the markup above, but this should get you started.
Upvotes: 1
Reputation: 65274
Since you use PHP, the best way to do this is to use an outer-inner-query loop:
Outer query: SELECT * FROM drings_category
, then loop:
SELECT drinks.drink_name FROM drinks INNER JOIN drinks_lookup ON drinks.drink_id=drinks_lookup.drink_id WHERE drinks_lookup.drink_category_id=$ID
Upvotes: 0