如果数据库中存在一个值,我该如何更新它,否则我想插入它
How can i update a value if it is exsiting in the database, or else i want to insert it
我有一个名为 PriceIK(priceCount,属性ID,bestPrice) 的 table。
现在我想插入一条记录以及 属性ID 和 bestPrice(priceCount 是自动递增的)
如果 属性ID 存在,我想 更新 相应的 bestPrice 值。
如果 属性 值不存在,我想 插入 新记录(属性ID 和 bestPrice)
- 我正在使用 php 和 phpMyAdmin
- 我是初学者,我真的需要你们的帮助
提前谢谢你
$sql="select * from PriceIK where propertyID='$_POST['property']'";
$query=mysqli_query($con,$sql);// Here `$con` is the DB connectivity variable
$num=$query->num_rows;// This will get the count of the executed query
if($num>0){
$up="Update PriceIK set bestPrice='$_POST['price']' where propertyID='$_POST['property']'";
$qry=mysqli_query($con,$up); // Here `$con` is the DB connectivity variable
}else{
$ins="Insert into PriceIK(propertyID,bestPrice) values('".$_POST['property']."','".$_POST['price']."')";
$qry=mysqli_query($con,$ins); // Here `$con` is the DB connectivity variable
}
试一试
使用INSERT ... ON DUPLICATE KEY UPDATE
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
UPDATE table SET c=c+1 WHERE a=1;
在PHP
中尝试以下代码
$sql = "SELECT * FROM PriceIK WHERE propertyID = " . $propertyId;
$result = $connection->query($sql);
$persistent_sql = '';
if ($result->num_rows > 0) {
$persistent_sql = "UPDATE PriceIK SET bestPrice = " . $bestPrice;
}
else {
$persistent_sql = "INSERT INTO PriceIK(propertyID, bestPrice) VALUES(".$propertyId.", ".$bestPrice.") ";
}
mysqli_query($connection, $persistent_sql)
据我了解,这就是你需要的东西。
INSERT INTO PriceIK (propertyID,bestPrice) VALUES(1 , 5000) ON DUPLICATE KEY UPDATE bestPrice="5000"
我默认使用 propertyID=1 和 bestPrice=5000
希望对您有所帮助
为了执行此操作,您可以更喜欢使用 MySQL INSERT ON DUPLICATE KEY UPDATE statement
。
INSERT ON DUPLICATE KEY UPDATE
是对 INSERT 语句的 MySQL 扩展。如果您在 INSERT 语句中指定 ON DUPLICATE KEY UPDATE
选项并且新行在 UNIQUE
或 PRIMARY KEY
索引中导致重复值,则 MySQL 会根据关于新值。
INSERT ON DUPLICATE KEY UPDATE
语句的语法如下:
INSERT INTO table(column_list)
VALUES(value_list)
ON DUPLICATE KEY UPDATE column_1 = new_value_1, column_2 = new_value_2, …;
唯一添加到 INSERT 语句的是 ON DUPLICATE KEY UPDATE 子句,您可以在其中指定逗号分隔的列分配列表。
MySQL returns 基于其执行的操作的受影响行数。
- 如果MySQL将该行作为新行插入,则受影响的行数为1。
- 如果MySQL更新当前行,则受影响的行数为2。
- 如果 MySQL 使用当前值更新当前行,则受影响的行数为 0。
插入语句:
INSERT INTO devices(name) VALUES ('Printer') ON DUPLICATE KEY UPDATE name = 'Printer';
这是一个插入语句,因为设备 table 不包含 Printer
值,因此它将插入它。
更新声明:
INSERT INTO devices(id,name) VALUES (4,'Printer') ON DUPLICATE KEY UPDATE name = 'Server';
这将更新 table,因为打印机已经存在,因此它会将 table 值更新为服务器。
我有一个名为 PriceIK(priceCount,属性ID,bestPrice) 的 table。
现在我想插入一条记录以及 属性ID 和 bestPrice(priceCount 是自动递增的) 如果 属性ID 存在,我想 更新 相应的 bestPrice 值。 如果 属性 值不存在,我想 插入 新记录(属性ID 和 bestPrice)
- 我正在使用 php 和 phpMyAdmin
- 我是初学者,我真的需要你们的帮助 提前谢谢你
$sql="select * from PriceIK where propertyID='$_POST['property']'";
$query=mysqli_query($con,$sql);// Here `$con` is the DB connectivity variable
$num=$query->num_rows;// This will get the count of the executed query
if($num>0){
$up="Update PriceIK set bestPrice='$_POST['price']' where propertyID='$_POST['property']'";
$qry=mysqli_query($con,$up); // Here `$con` is the DB connectivity variable
}else{
$ins="Insert into PriceIK(propertyID,bestPrice) values('".$_POST['property']."','".$_POST['price']."')";
$qry=mysqli_query($con,$ins); // Here `$con` is the DB connectivity variable
}
试一试
使用INSERT ... ON DUPLICATE KEY UPDATE
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
UPDATE table SET c=c+1 WHERE a=1;
在PHP
中尝试以下代码$sql = "SELECT * FROM PriceIK WHERE propertyID = " . $propertyId;
$result = $connection->query($sql);
$persistent_sql = '';
if ($result->num_rows > 0) {
$persistent_sql = "UPDATE PriceIK SET bestPrice = " . $bestPrice;
}
else {
$persistent_sql = "INSERT INTO PriceIK(propertyID, bestPrice) VALUES(".$propertyId.", ".$bestPrice.") ";
}
mysqli_query($connection, $persistent_sql)
据我了解,这就是你需要的东西。
INSERT INTO PriceIK (propertyID,bestPrice) VALUES(1 , 5000) ON DUPLICATE KEY UPDATE bestPrice="5000"
我默认使用 propertyID=1 和 bestPrice=5000
希望对您有所帮助
为了执行此操作,您可以更喜欢使用 MySQL INSERT ON DUPLICATE KEY UPDATE statement
。
INSERT ON DUPLICATE KEY UPDATE
是对 INSERT 语句的 MySQL 扩展。如果您在 INSERT 语句中指定 ON DUPLICATE KEY UPDATE
选项并且新行在 UNIQUE
或 PRIMARY KEY
索引中导致重复值,则 MySQL 会根据关于新值。
INSERT ON DUPLICATE KEY UPDATE
语句的语法如下:
INSERT INTO table(column_list)
VALUES(value_list)
ON DUPLICATE KEY UPDATE column_1 = new_value_1, column_2 = new_value_2, …;
唯一添加到 INSERT 语句的是 ON DUPLICATE KEY UPDATE 子句,您可以在其中指定逗号分隔的列分配列表。
MySQL returns 基于其执行的操作的受影响行数。
- 如果MySQL将该行作为新行插入,则受影响的行数为1。
- 如果MySQL更新当前行,则受影响的行数为2。
- 如果 MySQL 使用当前值更新当前行,则受影响的行数为 0。
插入语句:
INSERT INTO devices(name) VALUES ('Printer') ON DUPLICATE KEY UPDATE name = 'Printer';
这是一个插入语句,因为设备 table 不包含 Printer
值,因此它将插入它。
更新声明:
INSERT INTO devices(id,name) VALUES (4,'Printer') ON DUPLICATE KEY UPDATE name = 'Server';
这将更新 table,因为打印机已经存在,因此它会将 table 值更新为服务器。