简体   繁体   English

在一列Mysql和PHP中显示多行

[英]Display multiple rows in one column Mysql and PHP

Im trying to display multiple row values in one column. 我试图在一列中显示多个行值。 Im using 4 tables which consist of a one to many relationship. 我正在使用由一对多关系组成的4个表。 I have built a query with the data I will need, Im not sure if the issue lyes here or actually on the PHP side. 我已经用所需的数据构建了一个查询,我不确定问题是在这里还是实际上在PHP方面。 I have included some code so you can get an idea. 我已包含一些代码,因此您可以了解一下。 I honestly think I may need to use GROUP BY or include a foreach loop after the while to get the result I want. 老实说,我可能需要在一段时间后使用GROUP BY或包含一个foreach循环才能获得所需的结果。 If someone could point me in the right direction or guide me to a solution it would be much appreciated. 如果有人可以指出正确的方向或指导我解决问题,将不胜感激。

I can stop Category duplication by using GROUP BY category_name, but Im having problems with the subCat_name not showing all the results. 我可以使用GROUP BY category_name停止类别重复,但是Im的subCat_name不能显示所有结果。 Only displays the first row. 仅显示第一行。 It should show all subCat_name under the main category_name 它应在主类别名称下显示所有subCat_name

Currently getting this 目前得到这个

Ideas (category_name)

Shop More( subCat_name)
Have a better wardrobe(subCat_description)

But it should be like 但这应该像

Ideas (category_name)

Shop More( subCat_name)
Have a better wardrobe(subCat_description)

Build a Blog( subCat_name)
Share Ideas(subCat_description)

Travel to NYC( subCat_name)
Book a holiday(subCat_description)

MYSQL QUERY MySQL查询

$query = 'SELECT project_users.*, project.project_name, category.category_name,     sub_category.subCat_name, sub_category.subCat_description 
FROM project_users 
JOIN project 
ON project.project_id = project_users.project_id 
JOIN category 
ON category.project_id = project.project_id 
JOIN sub_category 
ON sub_category.category_id = category.category_id';

PHP 的PHP

$result = mysqli_query($link, $query);

if($result) {

    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

        echo '<!-- category container for tasks-->
              <div class="col-lg-4 category">
               <section class="panel">
                <div class="panel-body">
                 <header class="to-do-head">';
            echo '<h3>' . $row['category_name'] . '</h3>';
            echo ' <a class="to-do pull-right"><i class="icon-plus"></i> Add Task</a>';
            echo '</header>';
            echo '<div class="col-lg-12 task"><!--open task-->';
            echo '<div class="panel-body">';
            echo '<h4>' . $row['subCat_name'] . '</h4>';
            echo '<p>' . $row['subCat_description'] . '</p>';
            echo '</div><!--close panel-body-->';
            echo '<div class="task-foot">';
            echo '<a href=""><i data-original-title="Add Action" class="icon-legal tooltips"></i></a>
                                 <p class="pull-right"><i class="icon-calendar"></i> 16th Jan 2014</p>
                                   </div><!--close task foot-->';
            echo '</div><!--close-lg-12 task-->';
            echo '</div><!--close panel body-->
                  </section><!--close panel-->
                  </div><!--col lg 4 close container for tasks-->'; 

    }//end while

You need some basic logic to detect when a category changes, and output appropriate html for that. 您需要一些基本的逻辑来检测类别何时更改,并为此输出适当的html。 eg 例如

$old_cat = null;
while($row = mysqli_fetch_array(...)) {
   if ($row['category'] != $old_cat) {
       ... output category header ...
       $old_cat = $row['category'];
   }
   ... output subcategory stuff here ...
}

EDIT: Updated for MySQL 编辑:为MySQL更新

Sorry, missed the note that this was MySQL. 抱歉,没有注意到这是MySQL。 Certainly GROUP_CONCAT would be the alternative to LISTAGG (and it's easier). 当然,GROUP_CONCAT是LISTAGG的替代品(而且更容易)。 I was having trouble with getting the line break character to work (from what I can gather, '\\r\\n' or CHAR(13,10) should give a line break, but I haven't had any luck in SQLFiddle). 我在使换行符起作用时遇到了麻烦(据我所知,'\\ r \\ n'或CHAR(13,10)应该给出换行符,但是SQLFiddle中没有运气)。

Line Breaks: http://www.askingbox.com/tip/mysql-line-breaks-in-mysql 换行符: http : //www.askingbox.com/tip/mysql-line-breaks-in-mysql

GROUP_CONCAT Separator Example: Aggregate function in MySQL - list (like LISTAGG in Oracle) GROUP_CONCAT分隔符示例: MySQL中的聚合函数-列表(如Oracle中的LISTAGG)

Here's what I came up with: 这是我想出的:

SELECT
    category.category_name || CHAR(13, 10)
    || GROUP_CONCAT((sub_category.subCat_name || CHAR(13,10) || subcat_description 
         separator CHAR(13,10)) -- if the separator gives trouble, remove it, use a REPLACE() function around the GROUP_CONCAT, and replace ',' with CHAR(13, 10)
    as list

FROM 
    project_users 
    JOIN project ON project.project_id = project_users.project_id 
    JOIN category ON category.project_id = project.project_id
    JOIN sub_category ON sub_category.category_id = category.category_id

Group By 
    category.category_name

I'm not sure if this will work. 我不确定这是否行得通。 The ouput I was getting was an error when trying to use the separator function, or '0' for the rows when using any type of line break. 尝试使用分隔符功能时,我得到的输出是一个错误,或者在使用任何类型的换行符时,该行的输出为'0'。 Report back and let me know so I can delete or change the answer as needed. 向我报告,让我知道,以便我可以根据需要删除或更改答案。


(PREVIOUS ANSWER, good for ORACLE) (上一个答案,对ORACLE有利)

For showing multiple rows of column_X in a single row, the below might be a useful workaround when using SQL. 为了在一行中显示column_X的多行,使用SQL时,以下方法可能是一个有用的解决方法。 Using LISTAGG and the newline character chr(10) will be your friend. 使用LISTAGG和换行符chr(10)将是您的朋友。

Table

Column Y      Column X
 1             a
 1             b
 1             c
 1             d
 2             foo
 2             bar

Code

SELECT column_Y, LISTAGG(column_X, chr(10)) WITHIN GROUP (ORDER BY column_X) 
FROM table
GROUP BY column_Y

Output 输出量

Column Y      Column X
 1             a
               b
               c
               d
 2             foo
               bar

Here's a site with additional examples to get you started if you like this method: 如果您喜欢此方法,那么这里是一个带有其他示例的网站,可帮助您入门:

http://www.techonthenet.com/oracle/functions/listagg.php http://www.techonthenet.com/oracle/functions/listagg.php

EDIT: 编辑:

I thought I would also mention the use of XMLELEMENT and XMLAGG . 我以为我还会提到XMLELEMENTXMLAGG的使用。 It was designed for XML, but can also be used with HTML since they both use tags. 它是为XML设计的,但也可以与HTML一起使用,因为它们都使用标签。 I don't use it often, so I can't give you a great example, but the documentation (see the following link) does a good job of showing you some of the power of XMLAGG. 我不经常使用它,所以我不能给您一个很好的例子,但是文档(请参阅以下链接)很好地向您展示了XMLAGG的强大功能。 You can also look for other examples online: 您也可以在线查找其他示例:

http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions215.htm http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions215.htm

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

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