[英]MySQL: How to update colum1 if value does not exists in column 2
I want to write a value into a column "email_new", but only if this value does not exist in a second column called "email". 我想在“email_new”列中写入一个值,但只有在第二列“email”中不存在该值时才会这样。 This means none of the entries in the column email should contain the new mail-address. 这意味着列电子邮件中的所有条目都不应包含新的邮件地址。
This is for a programm to change the email-address, where the email-address has to be verified, and the column "email" allows only UNIQUE values. 这是为了让程序更改电子邮件地址,其中必须验证电子邮件地址,并且“email”列仅允许UNIQUE值。
"
UPDATE `table`
SET email_new = '".mysql_real_escape_string($newmail)."'
WHERE
user = '".mysql_real_escape_string($user)."'
&& email NOT CONTAINS('".mysql_real_escape_string($newmail)."')
"
Is something like that possible? 有可能吗?
$q = "
UPDATE
`table`
SET
`email_new` = '" . mysql_real_escape_string($newmail) . "'
WHERE
`user` = '" . mysql_real_escape_string($newmail) . "'
AND NOT EXISTS (
SELECT
`email`
FROM
`table`
WHERE
`email` = '" . mysql_real_escape_string($newmail) . "'
)
";
UPDATE table
SET email_new = ?
WHERE
user = ?
AND (SELECT COUNT(*) = 0 FROM table WHERE email = ?)
Replace ?
替换?
signs by actual values. 按实际值的符号。
you are not checking wheter the newmail is really unique: 你没有检查新邮件是否真的独一无二:
append to your query another AND condition, something like: AND 0 = (select count(*) from table where email = mysql_real_escape_string($newmail)) 向你的查询附加另一个AND条件,例如:AND 0 =(从表中选择count(*),其中email = mysql_real_escape_string($ newmail))
Or simply 或者干脆
UPDATE `table`
SET email_new = IF (email IS NULL, ?, ?)
-- this line is if you want to update email if email is not null
, email = IF (email IS NOT NULL, ?, ?)
WHERE ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.