在 mysql 中使用网络抓取工具为数据库插入编写触发器......不断收到不同的错误
Writing a trigger for a database insert using a webscraper in mysql.... keep getting different errors
虽然我对 MySQL 有一些经验,但我不是很有经验。我有一个问题想用触发器解决,但事实证明它比我想象的要复杂得多,希望得到一些建议。
我有两个 table。表 A 和表 B。
我们收到了客户的数据请求。每一批新的请求都被网络爬虫抓取(这是我们唯一能做到的方法,所以忽略这个过程听起来有多奇怪),转储到 A,然后它应该去 B,摆脱重复,然后根据数据向他们发送电子邮件。我看不到网络抓取工具是如何插入数据的,所以这已经结束了。
因为客户提交多个请求或者同一个人有不同的请求,所以数据需要唯一,但不是那么唯一。我们希望将每个请求记录为一个独特的请求,即使它来自同一客户。有些客户分享了一个名字,或者他们回来提出了不同的要求。
因此我让 table B 具有唯一的主键:姓名、电子邮件、地址和备注。 (如果我对唯一索引的看法是正确的,那么任何匹配的索引都会更新,如果有两个 John Smiths,这会很糟糕,所以它是主键)。
我已经尝试过不同的方法来做到这一点,在整个网站的多个线程上遵循示例,但是我已经在这个问题上好几天了,我正在失去它!!!!我知道我做错了什么,但是什么?!我最终做的是这样的:
在 TABLEA 上触发,AFTER_INSERT:
tableA_to_email
(
customer_name, customer_email, customer_phone, customer_address)
VALUES ((
SELECT
new.customer_name
FROM
tableA
WHERE
customer_name = new.customer_name),
(
SELECT
new.customer_email
FROM
tableA
WHERE
customer_email = new.customer_email),
(
SELECT
new.customer_phone
FROM
tableA
WHERE
customer_phone = new.customer_phone),
(
SELECT
new.customer_address
FROM
tableA
WHERE
customer_address = new.customer_address))
ON DUPLICATE KEY UPDATE
customer_phone = VALUES(customer_phone)
输入一个空的table: insert INTO tableA (customer_name
, customer_phone
, customer_email
, customer_address
) VALUES( "7", "0", "8", "0");
输出:MySQL 表示:文档
1242 - 子查询 returns 多于 1 行
我理解错误,但是上面的输入不超过一行?我在空的 table 上试了一下,所以...
基本上,在您的方法中,您只能从您所做的所有 select 中输入 1 个结果,显然您得到的结果不止一个成员
所以
INSERT INTO tableA_to_email
(
customer_name, customer_email, customer_phone, customer_address)
VALUES ((
SELECT
new.customer_name
FROM
tableA
WHERE
customer_name = new.customer_name LIMIT 1),
(
SELECT
new.customer_email
FROM
tableA
WHERE
customer_email = new.customer_email LIMIT 1),
(
SELECT
new.customer_phone
FROM
tableA
WHERE
customer_phone = new.customer_phone LIMIT 1),
(
SELECT
new.customer_address
FROM
tableA
WHERE
customer_address = new.customer_address LIMIT 1))
ON DUPLICATE KEY UPDATE
customer_phone = VALUES(customer_phone)
请问运行没有问题因为每个selectreturns只有1行.
但是你只能做
INSERT INTO tableA_to_email
( customer_name, customer_email, customer_phone, customer_address)
VALUES (
new.customer_name,
new.customer_email,
new.customer_phone,
new.customer_address
)
ON DUPLICATE KEY UPDATE
customer_phone = VALUES(customer_phone)
同样有效
虽然我对 MySQL 有一些经验,但我不是很有经验。我有一个问题想用触发器解决,但事实证明它比我想象的要复杂得多,希望得到一些建议。
我有两个 table。表 A 和表 B。
我们收到了客户的数据请求。每一批新的请求都被网络爬虫抓取(这是我们唯一能做到的方法,所以忽略这个过程听起来有多奇怪),转储到 A,然后它应该去 B,摆脱重复,然后根据数据向他们发送电子邮件。我看不到网络抓取工具是如何插入数据的,所以这已经结束了。
因为客户提交多个请求或者同一个人有不同的请求,所以数据需要唯一,但不是那么唯一。我们希望将每个请求记录为一个独特的请求,即使它来自同一客户。有些客户分享了一个名字,或者他们回来提出了不同的要求。
因此我让 table B 具有唯一的主键:姓名、电子邮件、地址和备注。 (如果我对唯一索引的看法是正确的,那么任何匹配的索引都会更新,如果有两个 John Smiths,这会很糟糕,所以它是主键)。
我已经尝试过不同的方法来做到这一点,在整个网站的多个线程上遵循示例,但是我已经在这个问题上好几天了,我正在失去它!!!!我知道我做错了什么,但是什么?!我最终做的是这样的:
在 TABLEA 上触发,AFTER_INSERT:
tableA_to_email
(
customer_name, customer_email, customer_phone, customer_address)
VALUES ((
SELECT
new.customer_name
FROM
tableA
WHERE
customer_name = new.customer_name),
(
SELECT
new.customer_email
FROM
tableA
WHERE
customer_email = new.customer_email),
(
SELECT
new.customer_phone
FROM
tableA
WHERE
customer_phone = new.customer_phone),
(
SELECT
new.customer_address
FROM
tableA
WHERE
customer_address = new.customer_address))
ON DUPLICATE KEY UPDATE
customer_phone = VALUES(customer_phone)
输入一个空的table: insert INTO tableA (customer_name
, customer_phone
, customer_email
, customer_address
) VALUES( "7", "0", "8", "0");
输出:MySQL 表示:文档
1242 - 子查询 returns 多于 1 行
我理解错误,但是上面的输入不超过一行?我在空的 table 上试了一下,所以...
基本上,在您的方法中,您只能从您所做的所有 select 中输入 1 个结果,显然您得到的结果不止一个成员
所以
INSERT INTO tableA_to_email
(
customer_name, customer_email, customer_phone, customer_address)
VALUES ((
SELECT
new.customer_name
FROM
tableA
WHERE
customer_name = new.customer_name LIMIT 1),
(
SELECT
new.customer_email
FROM
tableA
WHERE
customer_email = new.customer_email LIMIT 1),
(
SELECT
new.customer_phone
FROM
tableA
WHERE
customer_phone = new.customer_phone LIMIT 1),
(
SELECT
new.customer_address
FROM
tableA
WHERE
customer_address = new.customer_address LIMIT 1))
ON DUPLICATE KEY UPDATE
customer_phone = VALUES(customer_phone)
请问运行没有问题因为每个selectreturns只有1行.
但是你只能做
INSERT INTO tableA_to_email
( customer_name, customer_email, customer_phone, customer_address)
VALUES (
new.customer_name,
new.customer_email,
new.customer_phone,
new.customer_address
)
ON DUPLICATE KEY UPDATE
customer_phone = VALUES(customer_phone)
同样有效