我需要帮助才能在此处提供最佳查询。我有以下数据库:
+----+--------------+-----------------+------------------------------+
| id | reference_id | reference_field | value |
+----+--------------+-----------------+------------------------------+
| 1 | 6215 | title | Best recipe |
| 2 | 6215 | introText | Intro for best recipe |
| 3 | 6215 | fullText | Full text for best recipe |
| 4 | 6216 | title | Play Football |
| 5 | 6216 | introText | Intro for play football |
| 6 | 6216 | fullText | Full text for play football |
+----+--------------+-----------------+------------------------------+
我需要通过reference_id进行查询,我应该通过reference_field打印值,输出信息示例:
Best recipe
Intro for best recipe
Full text for best recipe
Play Football
Intro for play football
Full text for play football
更新 为此,我将使用PHP以下列方式打印查询:
$result = $config->mysqli->query("SELECT * FROM table_name ORBER BY reference_id");
while($row = $result->fetch_array()) {
echo ('<h1>'.$row["title"].'</h1>');
echo ('<h1>'.$row["introText"].'</h1>');
echo ('<h1>'.$row["fullText"].'</h1>');
}
通过上面的查询,我逐个获取所有记录(未按reference_id分组),另一方面,如果我进行查询
SELECT * FROM table_name GROUP BY reference_id
如何在PHP中的循环交互中获取3个值(title,introText,fullText)?
正如您所看到的那样,正常的“order by”或“group by”不会产生正确的结果来打印循环上的值。我在这里看到的是,结果我应该通过PHP 中的每个循环交互打印 3条记录的值,而不是打印每条记录的3个字段,这有意义吗?
答案 0 :(得分:0)
我认为这就是你所需要的:
SELECT value from TABLE_NAME
ORDER BY reference_id
答案 1 :(得分:0)
你可能想要这个:
select *
from your_table
order by reference_id, reference_field desc;
答案 2 :(得分:0)
创建连接,执行查询并显示结果:
<?php
$dbo = //Create db connection
$statement = $dbo->prepare("SELECT * FROM table_name ORBER BY reference_id");
$statement->execute();
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
echo "<pre>";
print_r ($result);
echo "</pre>";
?>