我无法弄清楚如何编写一个SQL查询来显示我想要的信息

时间:2013-05-25 19:33:48

标签: php mysql sql database

我已经为我的网站设置了数据库,我对如何编写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

这让我得到了所有的信息,但我不知道如何编写输出来让它显示我想要的。

3 个答案:

答案 0 :(得分: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列表而不是上面的标记,但这应该可以让您入门。

答案 1 :(得分:0)

由于您使用PHP,最好的方法是使用外部内部查询循环:

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

  • 显示drink_category_title
  • 将drink_category_id用作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
  • 的$ ID
  • 循环和展示饮品

答案 2 :(得分: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中进行格式化,但很无聊