根据加入表的结果更新MySQL表

时间:2014-07-11 22:12:20

标签: mysql email sql-update subquery left-join

我有三个表,电子邮件,person_details和data_providers。基本上我的所有用户ID,电子邮件和当前分配的data_providers_id都存储在电子邮件表中。

第二个表person_details包含由多个数据提供者收集的人口统计信息,每一行都由emails_id标识,该信息与emails.id data_providers_id相关,与第三个表data_providers.id有关。

第三个表data_providers包含我的每个数据提供者ID,名称和优先级。

基本上,用户信息可以从多个来源收集,我需要UPDATE emails set data_providers_id =基于一个选择,它将加入person_details表,data_providers表按data_providers.precedence DESC排序,然后是person_details.import_date ASC和使用第一个值(最高优先级,然后是最旧的import_date)。

我试图构建查询,但我的子查询返回多行。这个问题有点过头了,希望对复杂查询更有经验的人能够指出我正确的方向。

UPDATE emails 
SET emails.data_providers_id = 
    SELECT person_details.data_providers_id 
    FROM person_details 
    LEFT JOIN data_providers ON person_details.data_providers_id = data_providers.id
    ORDER BY data_providers.percent_payout ASC, person_details.import_date ASC ;

如果有帮助,这里有三个表的一些细节。任何指导都将非常感谢。在此先感谢:)

电子邮件表:

+-------------------+---------------------+------+-----+---------------------+----------------+
| Field             | Type                | Null | Key | Default             | Extra          |
+-------------------+---------------------+------+-----+---------------------+----------------+
| id                | int(11) unsigned    | NO   | PRI | NULL                | auto_increment |
| data_providers_id | tinyint(3) unsigned | NO   | MUL | NULL                |                |
| email             | varchar(255)        | NO   | UNI | NULL                |                |
+-------------------+---------------------+------+-----+---------------------+----------------+

person_details:

+-------------------+---------------------+------+-----+---------------------+-------+
| Field             | Type                | Null | Key | Default             | Extra |
+-------------------+---------------------+------+-----+---------------------+-------+
| emails_id         | int(11) unsigned    | NO   | PRI | NULL                |       |
| data_providers_id | tinyint(3) unsigned | NO   | PRI | NULL                |       |
| fname             | varchar(255)        | YES  |     | NULL                |       |
| lname             | varchar(255)        | YES  |     | NULL                |       |
| address_line1     | text                | YES  |     | NULL                |       |
| address_line2     | text                | YES  |     | NULL                |       |
| city              | varchar(255)        | YES  |     | NULL                |       |
| state             | varchar(2)          | YES  |     | NULL                |       |
| zip5              | varchar(5)          | YES  |     | NULL                |       |
| zip4              | varchar(4)          | YES  |     | NULL                |       |
| home_phone        | varchar(10)         | YES  |     | NULL                |       |
| mobile_phone      | varchar(10)         | YES  |     | NULL                |       |
| work_phone        | varchar(10)         | YES  |     | NULL                |       |
| dob               | date                | YES  |     | NULL                |       |
| gender            | varchar(1)          | YES  |     | NULL                |       |
| ip_address        | varchar(15)         | NO   |     | NULL                |       |
| source            | varchar(255)        | NO   |     | NULL                |       |
| optin_datetime    | datetime            | NO   | MUL | NULL                |       |
| import_date       | timestamp           | NO   |     | 0000-00-00 00:00:00 |       |
+-------------------+---------------------+------+-----+---------------------+-------+

data_providers表:

+-----------------+---------------------+------+-----+---------+----------------+
| Field           | Type                | Null | Key | Default | Extra          |
+-----------------+---------------------+------+-----+---------+----------------+
| id              | tinyint(3) unsigned | NO   | PRI | NULL    | auto_increment |
| name            | varchar(255)        | NO   |     | NULL    |                |
| precedence      | int(2)              | YES  |     | 0       |                |
+-----------------+---------------------+------+-----+---------+----------------+

1 个答案:

答案 0 :(得分:0)

要使用SELECT作为表达式,您必须将其放在括号中。要获得第一个值,请使用LIMIT 1

UPDATE emails 
SET emails.data_providers_id = (
    SELECT person_details.data_providers_id 
    FROM person_details 
    LEFT JOIN data_providers ON person_details.data_providers_id = data_providers.id
    WHERE person_details.emails_id = emails.id
    ORDER BY data_providers.percent_payout ASC, person_details.import_date ASC
    LIMIT 1) ;