SELECT
vl1.phone_number,
vl1.first_name,
CONCAT(
SUBSTRING(
(
SELECT
vl2.phone_number
FROM
list as vl2
WHERE
vl2.phone_number LIKE CONCAT( SUBSTRING( vl1.phone_number FROM 1 FOR 3 ), "%" )
ORDER BY
RAND( )
LIMIT 1
)
FROM
1 FOR 6
),
FLOOR( RAND( ) * ( 8999 ) ) + 1000
) AS autogenNumber
FROM
list as vl1
LIMIT 1
我得到的结果是
phone_number | firstname | autogenNumber
autogenNumber是通过首先搜索共享前三个数字的其他编号来生成的。然后从该数字中选出6个数字,然后将另外4个随机数字替换为末尾。
上面的sql查询完全根据我的需要生成自动生成号。
但是,现在,当我想使用下面的类似查询更新此列表中的列security_phrase时,就会出现此问题。
UPDATE list as vl1
SET vl1.security_phrase = (
CONCAT(
SUBSTRING(
(
SELECT
vl2.phone_number
FROM
list AS vl2
WHERE
vl2.phone_number LIKE CONCAT( SUBSTRING(phone_number FROM 1 FOR 3 ), "%" )
ORDER BY
RAND( )
LIMIT 1
)
FROM
1 FOR 6
),
FLOOR( RAND( ) * ( 8999 ) ) + 1000
)
)
LIMIT 10
给我一个错误:
1093-表'vl1'被指定了两次,均作为'UPDATE'的目标 并作为单独的数据源
我也尝试过
UPDATE list AS vl1
JOIN list AS vl2
SET vl1.security_phrase = (
CONCAT( SUBSTRING( vl2.phone_number FROM 1 FOR 6 ), FLOOR( RAND( ) * ( 8999 ) ) + 1000 )
)
WHERE
vl2.phone_number LIKE CONCAT( SUBSTRING( vl1.phone_number FROM 1 FOR 3 ), "%" )
不起作用,也没有得到预期的结果...
任何帮助
答案 0 :(得分:1)
MySQL不允许在另一个子查询中再次引用表正在更新,除非 位于FROM
子句(Derived Table中)。
现在,在您的特殊情况下,我们需要将完整的SELECT
查询块放入“派生表”中。如聊天中所述,lead_id
是您的主键,因此我们将使用PK重新加入以相应地更新行。
UPDATE list AS t1
JOIN
(
SELECT
vl1.lead_id,
CONCAT(
SUBSTRING(
(
SELECT
vl2.phone_number
FROM
list as vl2
WHERE
vl2.phone_number LIKE CONCAT( SUBSTRING( vl1.phone_number FROM 1 FOR 3 ), "%" )
ORDER BY
RAND( )
LIMIT 1
)
FROM
1 FOR 6
),
FLOOR( RAND( ) * ( 8999 ) ) + 1000
) AS autogenNumber
FROM
list as vl1
) AS dt
ON dt.lead_id = t1.lead_id
SET t1.security_phrase = dt.autogenNumber