MySql database inserting values from another table based on conditions -
i have slight trouble splitting old table 100 k of records. table used collect orders ecommerce app. each order added table new row.
table called "customer_informations" described this:
+-------+-----------------------+------------+---------------------+---------------------+----------------+ | id | customer_phone_number | first_name | last_name | address | order_number | +-------+-----------------------+------------+---------------------+---------------------+----------------+
as mentioned, each row separate order. there lot of repeat customers. rebuilding structure, selected rows distinct based on customer_phone_number , inserted result test_informations, looks this:
+------+---------------------+------------+-----------+-------------+ | id | created_at | first_name | last_name | phone | +------+---------------------+------------+-----------+-------------+
this table represents customer accounts. values of id field copied on original table. now, harvest addresses customers had in customer_informations table.
the table holding customer addresses looks this:
+----+----------+----------+------+-------+-----+-------------------------+ | id | address1 | address2 | city | state | zip | customer_information_id | +----+----------+----------+------+-------+-----+-------------------------+
this problem is. select distinct addresses customer_information table, , hook them test_informations table ( through customer_information_id foreign key). how can approach this?
i tried following statement:
insert `la`.`test_addresses` ( `created_at`, `address1`, `address2`, `city`, `zip`, `state`, `customer_information_id`) select str_to_date(order_placed, '%m/%d/%y %t') created_at, `address`, '', `city_state_zip`, `zip`, `state`, if( exists( select `id` `test_informations` `customer_info`.`id` = `test_informations`.`id`), `customer_info`.`id`, (select `test_informations`.`id` `test_informations` `test_informations`.`phone` = `customer_info`.`customer_phone_number`)) customer_information_id `customer_info` group `address`
so i'm processing each row old table. if id of current row in temp_informations, add address foreign key set id of temp_informations entry. if id of row i'm processing not in test_informations, means must alternative address , need hook the account. bottom line is, false condition makes entire query go infinity.
i'm not neccesarily looking exact answer problem ( although nice). can guys point me right direction, should answer, functionality of mysql should into, or information on how split tables efficiently ?
edit:
tables visualized not exact copies of actual tables ( have more fields, included important ones simplicity ).
why using 2 tables (test_informations , 1 address data)? think it's better place data in 1 table, unless 1 user has many cities, states, zip codes...
it's not bad use one-to-one relationships, here have 2 tables same entity , think using not approach. so, merge , don't have implement foreign keys.
if have attributes cause problems, can normalize them separately.
also, take care different variations of same input, example clean '-', '/' , space characters phone number before validation.
Comments
Post a Comment