简体   繁体   English

MySQL查询按字段分组并打印分组的字段

[英]Mysql query to group by field and print grouped fields

I need help to make the best query posible here. 我需要帮助以使最佳查询成为可能。 I have the following Database: 我有以下数据库:

+----+--------------+-----------------+------------------------------+
| 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 |
+----+--------------+-----------------+------------------------------+

I need to make a query where I group by reference_id and I should print the value by the reference_field, example of the output info: 我需要进行查询,在其中我按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

UPDATE To accomplish this, I will print the query on the following way with PHP: 更新为实现此目的,我将通过以下方式使用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>');
}

With the query above, I get all the records one by one (not grouped by the reference_id), in other hand if I do the query 通过上面的查询,我一一获得了所有记录(未按reference_id分组),另一方面,如果我执行了查询

SELECT * FROM table_name GROUP BY reference_id

How do I get the 3 values (title, introText, fullText) to print on the loop interaction in PHP? 如何获取3个值(title,introText,fullText)以在PHP的循环交互中进行打印?

As you can see with a normal "order by" or "group by" does not produce the proper result to print the values on the loop. 如您所见,使用普通的“ order by”或“ group by”不会产生正确的结果来在循环上打印值。 What I see here is that on the result I should print the values of 3 records by each loop interaction in PHP instead print 3 fields of each records, does it make sense? 我在这里看到的是,在结果上我应该通过PHP中的每个循环交互来打印3 条记录的值, 而不是打印每条记录的3个字段 ,这有意义吗?

I think this is what you need: 我认为这是您需要的:

SELECT value from TABLE_NAME
ORDER BY reference_id

You may want this: 您可能想要这样:

select *
from your_table
order by reference_id, reference_field desc;

Create connection, do query and show the results: 创建连接,进行查询并显示结果:

<?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>";

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM