澄清我想要完成的事情: 更新“用户名”,如果有相同电子邮件但没有名称的记录。如果用户已有名称,则不要进行任何更改
我有一个网站,所有用户都可以成为“订阅者,企业家或投资者”。如果订阅者(我以前只是要求他们发送电子邮件)选择上传商业创意,那么该人可能会使用与之前相同的电子邮件地址,只需添加一个名称。所以我想INSERT INTO,如果电子邮件已经存在 - 添加名称,但只有在那里没有名称的情况下(这样一个人不能简单地过去写别人的详细信息)。
我已经走到这一步了:
mysql_query("
INSERT INTO users
(email, name)
VALUES
('" .$epost. "', '" .$namn. "')
ON DUPLICATE KEY UPDATE
name=VALUES(name) -->[if name == '', else do nothing...]
");
现在,如果不同,它会用新的名称替换当前名称。
我搜索“重复更新,如果字段为空” 并发现: http://forums.aspfree.com/sql-development-6/on-duplicate-key-update-but-only-if-value-isn-t-482012.html(合并?)
conditional on duplicate key update(更接近,但如果字段不同,我不想更新它。)
http://bytes.com/topic/php/answers/914328-duplicate-key-update-only-null-values(如果输入为空......那就不是了)
http://boardreader.com/thread/Insert_on_duplicate_key_update_only_if_c_can8Xachr.html(此页面的布局让我迷失了方向)
http://www.digimantra.com/tutorials/insert-update-single-mysql-query-duplicate-key/(只是更新)
我相信这可能有点我正在追求什么(?)http://thewebfellas.com/blog/2010/1/18/conditional-duplicate-key-updates-with-mysql但是我还没有设法让它发挥作用
此代码:
mysql_query("UPDATE users SET name='".$namn."'
WHERE email='".$epost."' AND name =''");
更新名称,只有当它之前为空时,这就是我之后的然而如果电子邮件尚不存在则不会插入新记录。
我的桌子
所以我尝试了这个:
mysql_query("
INSERT INTO users
SELECT email, 'victoria' FROM users
WHERE email='victoria@hejsan.se' ON DUPLICATE KEY UPDATE name = 'victoria'
");
我试过这个:
mysql_query("
INSERT INTO users
SELECT email, 'yay' from users
WHERE email='victoria@hejsan.se'
ON DUPLICATE KEY
UPDATE name = values(name)
");
来自@Fluffeh的回答
但没有任何反应。我误解了答案吗?
如果有人已经拥有一个帐户,那么他们也应该拥有密码,所以我只是要求他们验证密码,并确保我们在插入电子邮件时进行AJAX调用。邮件,看看电子邮件是否已经注册:)所以这不再是一个问题,我认为这是我将采用的解决方案。 !)
答案 0 :(得分:1)
使用insert .... on duplicate key...
语法解决问题的唯一方法是name
和email
制作复合键 - 我认为您最好使用auto_increment作为主键。
你可能需要先在PHP中加入一点逻辑来进行检查,然后插入或更新 - 或编写一个函数来为你做同样的测试 - 但这两个都不是一个简单的查询,你可以开始
编辑:不确定这是否是您想要做的,但我认为满足您要求的最佳解决方案是在一对多关系中实际使用两个表。
Table-Users
id | email | name
create table users(
id int(10) not null auto_increment,
email varchar(100),
name varchar(100),
primary key(email, name)
);
Table-Ideas
id | userID | idea
create table users(
id int(10) not null auto_increment primary key,
userID int(10) not null,
idea text
);
使用桌面上的主键,您可以安全地执行insert... duplicate...
,而无需担心会过多地写人。然而,第二个表格允许您将安全存储的想法锁定给用户,并让您为每个用户提供一些想法。由于关系是users.id
到ideas.userID
,即使他们的详细信息已更新,您也不会失去拥有它的人。
编辑:(又名,ZOMG facepalm )
$query="
update users
set name='".$userName."'
where
email='".$userEmail."'
and name is null";
编辑2 :(又名,擦眉头)
insert into users
select email, '".$namn."' from users where email='".$epost."'
on duplicate key
update name = values (name);
在这里工作:
mysql> create table test1 (myName varchar(10) unique, myEmail varchar(10));
Query OK, 0 rows affected (0.02 sec)
mysql> insert into test1 values ('Tom','something');
Query OK, 1 row affected (0.01 sec)
mysql> insert into test1 values('Nick',null);
Query OK, 1 row affected (0.00 sec)
mysql> select * from test1;
+--------+-----------+
| myName | myEmail |
+--------+-----------+
| Tom | something |
| Nick | NULL |
+--------+-----------+
2 rows in set (0.00 sec)
mysql> insert into test1 select myName, myEmail from test1
where myName='Tom' on duplicate key update myEmail = values (myEmail);
Query OK, 0 rows affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> select * from test1;
+--------+-----------+
| myName | myEmail |
+--------+-----------+
| Tom | something |
| Nick | NULL |
+--------+-----------+
2 rows in set (0.00 sec)
mysql> insert into test1 select 'Tom', myEmail from test1
where myName='Tom' on duplicate key update myEmail = values (myEmail);
Query OK, 0 rows affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> select * from test1;
+--------+-----------+
| myName | myEmail |
+--------+-----------+
| Tom | something |
| Nick | NULL |
+--------+-----------+
2 rows in set (0.00 sec)
mysql> insert into test1 select myName, 'Something Else' from test1
where myName='Tom' on duplicate key update myEmail = values (myEmail);
Query OK, 2 rows affected, 1 warning (0.01 sec)
Records: 1 Duplicates: 1 Warnings: 1
mysql> select * from test1;
+--------+------------+
| myName | myEmail |
+--------+------------+
| Tom | Something |
| Nick | NULL |
+--------+------------+
2 rows in set (0.00 sec)
mysql> insert into test1 select myName, null from test1
where myName='Nick' on duplicate key update myEmail = values (myEmail);
Query OK, 0 rows affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> select * from test1;
+--------+------------+
| myName | myEmail |
+--------+------------+
| Tom | Something |
| Nick | NULL |
+--------+------------+
2 rows in set (0.00 sec)
mysql> insert into test1 select myName, 'yay' from test1
where myName='Nick' on duplicate key update myEmail = values (myEmail);
Query OK, 2 rows affected (0.01 sec)
Records: 1 Duplicates: 1 Warnings: 0
mysql> select * from test1;
+--------+------------+
| myName | myEmail |
+--------+------------+
| Tom | Something |
| Nick | yay |
+--------+------------+
2 rows in set (0.00 sec)
编辑3:为$query
insert into table1 select coalesce(email,'".$epost."') as email, coalesce(name,'".$namn."') as name from table1
where email='".$epost."' on duplicate key update name = values (name);
答案 1 :(得分:1)
如果要更新已存在的字段,可以这样做;
-- MySQL Reference INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6) ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b); -- so your code INSERT INTO users (email, name) VALUES ('" .$epost. "', '" .$namn. "') ON DUPLICATE KEY UPDATE email=VALUES(email), name=VALUES(name)
http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
<强>更新强>
此时,您需要“MySQL条件插入”。我们来试试吧;
CREATE TABLE IF NOT EXISTS `users` ( `name` varchar(50) NOT NULL, `email` varchar(255) NOT NULL, UNIQUE KEY `email` (`email`) ) INSERT INTO users (name, email) VALUES('kerem', 'qeremy[atta]gmail[dotta]com'); INSERT INTO users (name, email) VALUES('test', 'test@example.com'); >> 2 rows inserted. ( Query took 0.0005 sec ) -- this is your part (regarding email field is UNIQUE, so WHERE email='search email') INSERT INTO users (name, email) SELECT 'test', 'test@example.com' FROM DUAL WHERE NOT EXISTS ( SELECT * FROM users WHERE email = 'test@example.com' LIMIT 1 ); >> 0 rows inserted. ( Query took 0.0003 sec )
https://www.google.com/search?q=conditional+insert+mysql
http://allurcode.com/2011/04/15/mysql-conditional-insert/(这是有用的搜索结果)
更新2
如果这对您不起作用,那么您可以像(仅作为输入)那样完成工作;
mysql_query("INSERT IGNORE INTO users (name, email) VALUES('$name', '$email')"); if (!mysql_insert_id()) { mysql_query("UPDATE users SET name='$name' WHERE name='' AND email='$email'"); } Or; mysql_query("UPDATE users SET name='$name' WHERE name='' AND email='$email'"); if (mysql_affected_rows() < 1) { mysql_query("INSERT INTO users (name, email) VALUES('$name', '$email')"); }
答案 2 :(得分:1)
我想使用控制功能IFNULL(exp1,exp2)建议这个解决方案,定义为&#34;如果exp1不为null,则返回exp1;如果exp1为null,则返回exp2&#34;。
http://dev.mysql.com/doc/refman/5.7/en/control-flow-functions.html
INSERT INTO users (name, email) VALUES ('$name','$email')
ON DUPLICATE KEY UPDATE name = IFNULL(name, VALUES(name))
当然,你必须以某种方式超越这个,但它帮助了我。