mysql 中的行差异
我想知道是否有一种简单的方法来比较几行 MySQL 数据。
特别是,我拥有的是一个包含每个用户的设置值列表的表。
用户可以毫无问题地在 GUI 中修改这些设置。
现在,我想做的是:每当用户保存新数据时,我想找到旧数据和要保存的数据之间的差异,以找出哪些列发生了更改,然后保存到日志中。 ,
我现在这样做的方法是在保存和比较之前读取与用户相对应的行,逐个变量地查找更改的数据,但我发现它很慢,我想知道是否有更聪明的方法来做到这一点 也许在 mysql 查询中(也许使用临时表?)或者通过使用一些我不知道的 php mysql 函数...
我希望你对我有一些想法。
(我检查了这个问题:https://stackoverflow.com/questions/218499/mysql-diff-tool ,但这与我正在寻找的有很大不同)
提前致谢!
I am wondering if there is an easy way to compare a couple of rows of MySQL data.
In particular, what i have is a table containing a list of setting values for each user.
A user can modify these settings in a gui in no problem.
Now, what i am trying to do is : whenever a user saves new data, i want to find the diff between the old data and the to-be-saved data to find out which columns where changed and later save to a log...
The way i am doing this right now is reading the row corresponding to the user before saving and comparing it, variable by variable to find the changed data but i find it slow and i am wondering if there is a smarter way to do this, perhaps inside a mysql query (maybe using a temporary table?) or by using some php mysql function i don't know about...
I hope you have some ideas for me.
(I checked this question : https://stackoverflow.com/questions/218499/mysql-diff-tool , but that turns out to be a lot different than what i am looking for)
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 PHP 中的
array_diff_assoc()
函数来完成此操作。这里我们有两行 php 数组数据。
它将返回一个数组,其中任何列值已更改或添加。
编辑
要在 MySQL 中执行此操作,您需要将它们放在名称值对中,如下所示:
TransactionID 1 是旧行,Transaction ID 2 是新行:
如果我的逻辑是正确的,应该屈服:
You can do this with the
array_diff_assoc()
function in PHP.Here we have two rows of data in php arrays.
It will return an array where any column values have changed or been added.
Edit
To do this in MySQL, you'd need them in name value pairs, something like this:
TransactionID 1 is the old row and Transaction ID 2 is the new row:
If my logic is right, should yield: