MySQL中如何比较两个查询结果是否相等?
我是 MySQL 用户。我有两个查询,我希望比较它们的结果是否相等。我想用一个返回 true 或 false 的查询来完成此操作,因此我的两个查询很可能采用子查询的形式。
我想避免返回两个查询的结果并在应用程序级别比较它们,以减少通信并提高性能。如果可能的话,我还想避免在数据库级别循环结果,但如果没有其他方法,那就这样吧。
我到处寻找有关如何执行此操作的示例,但却空手而归。一些示例代码将非常感激,因为我是 SQL 编程的新手。谢谢!
注意:我正在寻找一种适用于任意两个查询的解决方案,因此我将避免发布我的两个查询恰好是什么。
I'm a MySQL user. I have two queries, and I wish to compare their results for equality. I would like to do this with a single query which would return true or false, so each of my two queries would most likely take the form of sub-queries.
I would like to avoid returning the results from both queries and comparing them at the application level, to cut down on communication and to improve performance. I would also like to avoid looping over the results at the database level if possile, but if there's no other way, so be it.
I have searched high and low for an example on how to do this, but have come up empty handed. Some sample code would be most appreciated, because I'm a newbie to SQL programming. Thanks!
Note: I'm looking for a solution which would work with any two arbitrary queries, so I'm going to refrain from posting what my two queries happen to be.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果 MySQL 支持 FULL OUTER JOIN ,这会更容易一些。还要注意,如果两个查询给出相同的结果,但顺序不同,则将被视为相等。
如果计数 = 0,则两个查询是同样
也因为我使用 UNION 重复项被删除。所以那里存在潜在的不准确性。
This would be a little easier if MySQL supported
FULL OUTER JOIN
also note that if the the two queries give the same results but in different order that will be deemed equivlantIf the count =0 then the two queries are the same
Also because I'm using UNION duplicates are being removed. So there's a potential inaccuracy there.
你不能在 MySQL 中执行 MINUS 操作,所以这里是如何在没有
以下情况的情况下执行 MINUS 操作:
You can't do MINUS in MySQL, so here's how to do it without:
That's given: