简体   繁体   中英

MySql database inserting values from another table based on conditions

I have a slight trouble splitting my old table with about 100 k of records. Table was used to collect orders from ecommerce app. Each order was added to the table as a new row.

Table called "customer_informations" described like this:

+-------+-----------------------+------------+---------------------+---------------------+----------------+
| id    | customer_phone_number | first_name | last_name           | address             | order_number   |
+-------+-----------------------+------------+---------------------+---------------------+----------------+

As mentioned, each row is a separate order. There is a lot of repeat customers. I am rebuilding the structure, so I selected rows that are distinct based on customer_phone_number and inserted the result into test_informations, which looks like this:

+------+---------------------+------------+-----------+-------------+
| id   | created_at          | first_name | last_name | phone       |
+------+---------------------+------------+-----------+-------------+

This table represents the customer accounts. Values of Id field are copied over from original table. Now, I would like to harvest all the addresses that customers had in customer_informations table.

The table holding customer addresses looks like this:

+----+----------+----------+------+-------+-----+-------------------------+
| id | address1 | address2 | city | state | zip | customer_information_id |
+----+----------+----------+------+-------+-----+-------------------------+

This is where my problem is. I would like to select all distinct addresses from customer_information table, and hook them up to my test_informations table ( through customer_information_id foreign key). How can I approach this?

I tried the following statement:

INSERT INTO `la`.`test_addresses`
(
`created_at`,
`address1`,
`address2`,
`city`,
`zip`,
`state`,
`customer_information_id`)

Select 
STR_TO_DATE(order_placed, '%m/%d/%y %T') AS created_at,
`address`,
'',
`city_state_zip`,
`zip`,
`state`,
IF( EXISTS( SELECT `id` FROM `test_informations` WHERE `customer_info`.`id` = `test_informations`.`id`),
`customer_info`.`id`,
(SELECT `test_informations`.`id` FROM `test_informations` WHERE `test_informations`.`phone` = `customer_info`.`customer_phone_number`)) 
as customer_information_id 
From `customer_info` group by `address`

So I'm processing each row from old table. If id of current row is in my temp_informations, I just add this address with foreign key set to id of temp_informations entry. If the id of row i'm currently processing is NOT in test_informations, it means that it must be a alternative address and I need to hook it up to the the account. Bottom line is, the false condition makes this entire query go into infinity.

I'm not neccesarily looking for exact answer to this problem ( although it would be nice). Can you guys point me into right direction, where should I even look for answer, what functionality of MySql should I look into, or some information on how to split tables like this efficiently ?

EDIT:

Tables visualized are not exact copies of actual tables ( they have more fields, but I included the most important ones for simplicity ).

Why you are using two tables (test_informations and another one for address data)? I think it's better to place all data in only one table, unless one user has many cities, states, zip codes...

It's not so bad to use one-to-one relationships, but here we have two tables for the same entity and I think that you are using not so good approach. So, just merge it and then you don't have to implement this by foreign keys.

If you have some attributes which could cause problems, you can normalize them separately.

Also, take care about different variations of the same input, for example clean '-', '/' and space characters from the phone number before validation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM