如果行包含重复数据,如何突出显示行? [英] How to highlight rows if they contain duplicate data?

查看:51
本文介绍了如果行包含重复数据,如何突出显示行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据:

+------+-------------------+--------------------+
| id   |            number |             colour |
+------+-------------------+--------------------+
| 1766 |                53 | red                |
| 1767 |                 3 | green              |
| 1768 |               202 | green              |
| 1769 |                52 | blue               |
| 1770 |                56 | orange             |
| 1771 |                90 | yellow             |
| 1772 |                28 | teal               |
| 1773 |               276 | purple             |
| 1774 |                23 | black              |
| 1775 |                23 | orange             |
+------+-------------------+--------------------+

colour 列中的重要列.我想显示以上所有行(在 HTML 表中),但我想突出显示任何具有重复颜色的行.即两行绿色和两行橙色.

The important column here in the colour column. I want to display all the above rows (in an HTML table) but I want to highlight any rows which have duplicate colours. i.e. the two green rows and the two orange rows.

理想情况下,我会得到一个额外的列,其中包含一个布尔值(或任何真正的值),我可以在显示表格时检查它.

Ideally I would end up with an extra column that would contain a boolean (or anything really) that I could check when displaying the table.

推荐答案

好吧,如果您没有太多数据,您不需要任何子查询或任何东西.

Well, you don't need any subqueries or anything if you don't have much data.

当您像这样显示它们时,只需散列每一行(或您需要检查的行的一部分):

Just hash each row (or part of the row you need to check) as you display them like this:

$hashes = array();

while ($row = mysql_fetch_assoc($result)) {
    $rowHash = md5(implode("", $row), true);
    $isDuplicate = isset($hashes[$rowHash]));
    <... printing row contents ...>
    $hashes[$rowHash] = true;
}

此解决方案将有效,直到您有太多行无法在页面上显示.那么你也可以在数据库中缓存哈希值.

This solution will work until you have too much rows for display on a page. Then you may also cache hash value in the database.

这篇关于如果行包含重复数据,如何突出显示行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆