gpt4 book ai didi

php - Mysql,如何对来自2个不同表的查询进行分组 "graphically"

转载 作者:行者123 更新时间:2023-11-29 08:15:31 24 4
gpt4 key购买 nike

我有 2 张 table :

  • 表 1) courses_cat(该表有一个“标题”字段)
  • 表2)类(class)(这个有一个“父”字段,我想用它来“分组”,这个“父”等于“标题”

所以我需要将表 2 的内容“以图形方式”放入表 1 中。

这是我的代码(两个表分开):

<?php
$query = $db->prepare('SELECT title, body, showeb, datapost FROM courses_cat WHERE showeb="si"');
$query->execute();
$query->store_result();
$query->bind_result($title, $body, $showeb, $datapost);
while ($query->fetch())

echo " <div class='pblock'><h2>$title</h2><pre>$body</pre></div> ";
?>

<?php
$query2 = $db->prepare('SELECT parent, title, body, showeb, datapost FROM courses WHERE showeb="si"');
$query2->execute();
$query2->store_result();
$query2->bind_result($parent, $title, $body, $showeb, $datapost);
while ($query2->fetch())

echo " <div id='courseblock'><a href='#'><span class='top'>$title</span><span class='content'>$shortbody</span></a></div>";
?>

最佳答案

我不确定我是否 100% 理解最终结果应该是什么,但我确信您可以将两个查询合并为一个;正确循环可能会解决您的问题:

SELECT cat.title as CatTitle, cat.body as CatBody, courses.title as CourseTitle, courses.body as CourseBody -- and any other fields you need like that
FROM courses_cat cat, courses
WHERE cat.parent = courses.title
GROUP BY cat.title

关于php - Mysql,如何对来自2个不同表的查询进行分组 "graphically",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20688933/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com