拿这张表:
id | field1 | foreign
1 abc 12
2 def 12
3 abc 13
4 def 13
我像这样选择这条记录:
"SELECT field1 FROM table WHERE foreign = 12"
和
"SELECT field1 FROM table WHERE foreign = 13"
现在考虑我的表,此选择返回完全相同的记录值。
我如何知道给定 2 个任意 SELECT 它们在 SQL 中返回相同的值?
PS2。我除了 field1 之外还有其他字段
PS3。我知道我可以通过 PHP 来完成,但是有没有纯 SQL 的方法?
最佳答案
您可以使用md5sum来确认2个结果集是否相等,这在比较两个大结果集时非常有用。只需在 mysql 控制台中使用以下命令:
寻呼机 md5sum -
这会将 MySQL 分页器设置为在运行每个查询后显示 MD5Sum。
如果您使用 PHP 运行每个查询,那么您可以使用此方法创建相同的效果:
// This method will return MD5 of a result set. Compared results will return the same MD5Sum if they have the same results, and are ordered in the same way.
function query_md5($sql) {
$rs = mysql_query($sql);
$num = mysql_num_fields($rs);
$str = "";
while ($obj = mysql_fetch_array($rs)) {
$i = 0;
while ($i < $num) {
// no need for spaces or formatting
$str .= $obj[$i++];
}
}
return md5($str);
}
希望这有帮助!
关于php - 如何检查一组记录是否相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5701578/