在执行之前,我怎么知道有多少行会更新受影响的 sql [英] how can I know how many rows will update sql affected before I execute it
问题描述
使用explain可以看到下面这个sql中将选择多少条记录:
whe can see how many record will be selected in this sql bellow using explain:
explain select * from products
但 expain 不适用于更新和删除命令.
在php中,我想使用sql更新表,我想检查天气这个sql只会更新行或多行,如果多行受到影响,则不允许运行此sql以防止表误操作.
那么在我执行之前,我怎么知道这个更新的 sql 会影响多少行呢?
but expain is not for update and delete command.
In php, I want to update an table using a sql , I want to check weather this sql will update only row or many rows , if many rows affected , this sql is not allow to run for preventing the table misoperation.
so how can I know how many rows will this update sql affected before I execute it?
update products set products_model = 'hello' where products_id = '1'
推荐答案
Run the query with
Run the query with
SELECT COUNT(id) AS will_affect_rows_count FROM products WHERE products_id = '1'
这将为您提供将受 UPDATE 查询影响的行数.
This will give you the count of the rows that will be affected by your UPDATE query.
我可以问一下,为什么您可以在此表中包含具有重复 products_id 的产品?
Might I ask why you can have products in this table with duplicated products_id's?
这篇关于在执行之前,我怎么知道有多少行会更新受影响的 sql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!