查找具有不同值的行

时间:2013-07-16 09:54:03

标签: mysql sql database

我有一份声明,提出了某些范围内的项目的详细信息。所有细节都假设是这样的..

 id | color |  shape | material |
----------------------------
 i45 | blue  | square | plastic  |
 i46 | blue  | square | plastic  |
 i47 | blue  | square | plastic  |

但是如果没有正确定义范围:

id | color |  shape | material |
 ----------------------------
i45 | blue  | square | plastic  |
i46 | blue  | square | plastic  |
i47 | blue  | square | plastic  |
i48 | red   | square | plastic  |

我需要查询以恢复具有与其他值不同的值的列名称。列名将显示在弹出窗口中。目前我有一个声明:

$chk = "SELECT DISTINCT
    color,
    shape,
    material,
FROM $table 
WHERE  id BETWEEN $StartID AND $EndID";
$res = mysqli_query($con, $chk) or die (mysqli_error($con));
$r = mysqli_num_rows($res);
if ($r > 1)

<script language="JavaScript">alert ("Some fields have different data")</script>

只发现是否有不同的值..但我是如何获取具有不同值的列名称的堆栈。 请帮忙..

1 个答案:

答案 0 :(得分:0)

尝试

SELECT count( DISTINCT color  ) as Color, count( DISTINCT shape  ) as shape, count( DISTINCT material) as Material
FROM $table;

现在那些值大于1的列的值与其他列不同。