【问题标题】:MySql: Using concat does not work, why?MySql:使用 concat 不起作用,为什么?
【发布时间】:2012-05-21 21:25:00
【问题描述】:
我有这个问题:
update sales_flat_quote set customer_email = (concat(select substring(md5(rand()) from 1 for 20), '@example.com'));
它给了我这个错误:3 你的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取在第 6 行 SQL1.sql 6 1 的 '' 附近使用的正确语法。我想将子字符串选择的结果与静态 @example.com 字符串连接起来。
查询有什么问题?
谢谢!
【问题讨论】:
标签:
mysql
substring
concat
【解决方案1】:
select substring(md5(rand()) from 1 for 20 返回结果集,而不是字符串。
做你想做的事的正确方法是update sales_flat_quote set customer_email = (concat(substring(md5(rand()) from 1 for 20), '@example.com'));
【解决方案2】:
要使用子选择,您必须将其括在括号中:
update sales_flat_quote set customer_email = concat(
(select substring(md5(rand()) from 1 for 20)),
'@example.com');
请注意,在这种情况下,您不需要使用子选择:rand() 将为每一行调用。