0

我已经为我的网站设置了数据库,但我有点困惑我应该如何编写 SQL 查询来显示我想要的信息。

这是我的数据库设置方式的图像以及我希望如何显示它的示例:

这是我为获取信息而编写的 SQL 查询(我愿意编写更好的解决方案)

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

这为我提供了所有信息,但我不确定如何编写输出以显示我想要的方式。

4

3 回答 3

1

一种解决方案是按类别订购:

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

这样,您就可以在 PHP 中循环,并在出现新类别时输出新标题。像这样的东西:

<?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'];
}
?>

您可能应该使用嵌套的 html 列表而不是上面的标记,但这应该可以帮助您入门。

于 2013-05-25T19:53:43.620 回答
0

由于您使用 PHP,因此最好的方法是使用 external-inner-query 循环:

外部查询:SELECT * FROM drings_category,然后循环:

  • 显示drink_category_title
  • 使用 drink_category_id 作为 $IDSELECT drinks.drink_name FROM drinks INNER JOIN drinks_lookup ON drinks.drink_id=drinks_lookup.drink_id WHERE drinks_lookup.drink_category_id=$ID
  • 循环和展示饮料
于 2013-05-25T19:38:53.903 回答
0

在单个 sql 语句中为 3 个固定列执行此操作的方法极其复杂,每个类别最多可处理 1000 种饮料。

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

未测试。最好在 php 中进行格式化,但很无聊

于 2013-05-25T20:15:20.113 回答