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

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

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

基本上,可以从多个来源收集用户信息,我需要基于选择将和data_providers表按照data_providers.precedence DESC然后person_details.import_date ASC进行联接的选择UPDATE emails set data_providers_id =并使用第一个值(最高优先级,然后是最早的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       |                |
+-----------------+---------------------+------+-----+---------+----------------+

最佳答案

要将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) ;

关于mysql - 根据结果​​更新MySQL表以加入表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24707378/

10-15 03:48