简体   繁体   English

PHP显示MySQL数据库中的数据并分组

[英]php display data from mysql database and group by

noob here, sorry if my question is not clear, but here it goes. 菜鸟在这里,对不起,如果我的问题不清楚,但是可以了。

I have a mysql database with 2 tables (accounts and staff) I want to display the "account_name" and underneath that a list of the staff's "name" who are assigned to that account. 我有一个带有2个表(帐户和人员)的mysql数据库,我想显示“ account_name”,并在下面显示分配给该帐户的人员“名称”的列表。

with the code below I get the "account_name" and only the first staff's "name" assigned to that account. 使用下面的代码,我得到“ account_name”,并且只有第一位职员的“名称”被分配给该帐户。 some of the accounts have 2 or more staff members assigned. 一些帐户分配了2个或更多的工作人员。

I get: 我得到:

Target (account_name) 目标(account_name)

staff1 ("name") staff1(“名称”)

WalMart ("account_name") 沃尔玛(“ account_name”)

staff1 ("name") staff1(“名称”)


I want: 我想要:

Target ("account_name") 目标(“ account_name”)

staff1 ("name") staff2 ("name") staff1(“名称”)staff2(“名称”)

WalMart ("account_name") 沃尔玛(“ account_name”)

staff1 ("name") staff2 ("name") staff3 ("name") staff1(“名称”)staff2(“名称”)staff3(“名称”)


any and all help will be greatly appreciated. 任何帮助都将不胜感激。

Alex 亚历克斯

code: 码:

$query = "SELECT staff.name, staff.drive_id, accounts.id, accounts.account_name
FROM staff
JOIN accounts
ON staff.drive_id = accounts.id
WHERE staff.drive_id = accounts.id
AND accounts.drive_date = CURDATE()
GROUP BY accounts.account_name";
$result = mysql_query($query);

while ($staff = mysql_fetch_array($result))
{

echo "<br />";
echo $staff['account_name'];
echo "<br /><br />";
echo $staff['name'];
echo "<br />";
}

Group by will reduce all results to one result per grouped field You were after order by 分组依据会将所有结果减少为每个分组字段一个结果

$query = "SELECT staff.name, staff.drive_id, accounts.id, accounts.account_name
                        FROM staff
                        JOIN accounts
                        ON staff.drive_id = accounts.id
                        WHERE staff.drive_id = accounts.id
                        AND accounts.drive_date = CURDATE()
                        ORDER BY accounts.account_name";

In your loup you can then check whether the account_name has changed comparde to the previous result, and first echo the account details, then the name. 然后,在您的浏览器中,您可以检查account_name是否已更改为与先前结果相比,并首先回显了帐户详细信息,然后是名称。

$result = mysql_query($query);
$account = '';

while ($staff = mysql_fetch_array($result))
{
if($account == $staff['account_name'])
  {
  echo "<br />";
  echo $staff['account_name'];
  $account = $staff['account_name'];
  }
echo "<br /><br />";
echo $staff['name'];
echo "<br />";
}

You need an operator to specify how to combine the account names, such as the following: 您需要操作员来指定如何组合帐户名称,例如:

$query = "SELECT staff.name, staff.drive_id, accounts.id,
                 GROUP_CONCAT(accounts.account_name SEPARATOR ' ')
          FROM staff
          JOIN accounts
          ON staff.drive_id = accounts.id
          WHERE staff.drive_id = accounts.id
          AND accounts.drive_date = CURDATE()
          ORDER BY accounts.account_name"

See MySql's group by functions for more details and options. 有关更多详细信息和选项,请参见MySql的按功能分组

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

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