dpuwov1487 2016-06-07 07:23
浏览 36
已采纳

零行与未更改记录的差异更新?

I just figured out the result of a MySQL query between updating no matched records and updating a matched record that already has the values has no differences?

I have a query like this:

UPDATE uri SET is_active = 1 WHERE f_site_id = '73' AND language_code = 'NL' AND additional = 'item' AND is_forwarder = '0'

If I run this query and there are matched records, but their data does not change, I get a result of 0 rows affected? If I don't have any records with the above WHERE selection, MySQL returns 0 affected records....

I need to know after the update if there are matched records or not. It doesn't really matter if the data in the record is already the same, but I need to know that there are matched records.

How do I achieve this?

  • 写回答

1条回答 默认 最新

  • dongya9904 2016-06-07 07:27
    关注

    Just use a separate SELECT query prior to Update:

    SELECT count(*) as count FROM uri WHERE f_site_id = '73' AND language_code = 'NL' AND additional = 'item' AND is_forwarder = '0'
    

    Counting rows is not a very resource-intensive task and you do not seem to have many complex conditions.

    If you want to optimize queries, then after the SELECT check for count value, and only run UPDATE query if it's >0. No sense in running Update if there's nothing to update.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图