简体   繁体   English

连接从数据库返回的相等值

[英]Joining equal values that were returned from a database

I'm using a database in MySQL and returning values ​​via PHP. 我在MySQL中使用数据库并通过PHP返回值。 I'm using the framework Zend2. 我正在使用Zend2框架。

I have the following table: 我有下表:

[ID] | [Name] | [Amount]
-----|--------|---------
1    | Alex   | 10
-----|--------|---------
2    | Bruno  | 5
-----|--------|---------
3    | Miguel | 6
-----|--------|---------
4    | Ana    | 5

I need to unite draws. 我需要团结一致。 In this case, instead of displaying 在这种情况下,不显示

在此处输入图片说明

the result should display 结果应显示

在此处输入图片说明

Code to show in HTML: 以HTML显示的代码:

<?php foreach ($rows as $row) : ?>
    <tr>
        <td><?php echo ++$i; ?></td>
        <td><?php echo $this->escapeHtml($row->amount); ?></td>
        <td><?php echo $this->escapeHtml($row->name); ?></td>
        <td><img src="<?php echo $row->imageMedal($i); ?>"/></td>
    </tr>
<?php endforeach; ?>

The Zend2 structure has not changed. Zend2的结构没有改变。

You can GROUP_BY amount and concatenate Name in the groups with the same amount , then you can order by amount from the greatest value and cut the first three rows : 您可以将GROUP_BY数量和相同amount的组中的Name连接起来,然后可以按amount从最大值排序,并削减前三行:

SELECT `amount`
     , GROUP_CONCAT(`Name` SEPARATOR ', ') AS `name`
FROM `table` 
GROUP BY `amount` 
ORDER BY `amount` DESC
LIMIT 0, 3

@KeplerBR,这是ZF2中用于您查询的解决方案: https ://gist.github.com/settermjd/3360ccabb2b761a6ee11

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

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