MYSQL query check if ID exist and copy column row values to another table related column names

user2114900 :

I am trying to save Mysql row data from one database table and copy values in new database table via PhpMyAdmin but there is an issue.

My lack of knowledge is result for asking advanced users for help here. Copy, join, merge, delete or else :D ..i am really not sure what is the best method to solve this.

TABLE 1 (old) has columns: id, product_id, identifier, content

  • id - we dont need it for this query
  • post_id - (INT) which is actually related to ID in TABLE 2
  • identifier - (VARCHAR) different row values such as quantity, color, discount, price1, price2 which are repeatable fields
  • content - *(LONGTEXT) content

    TABLE 1

TABLE 2 (new) has columns:

  • id - (INT)
  • quantity - (VARCHAR)
  • color - (VARCHAR)
  • discount - (VARCHAR)
  • price1 - (VARCHAR)
  • price2 - (VARCHAR)

I need to check with mysql query does id in TABLE 2 exist compared with post_id from TABLE 1.

If does not exist skip to another record.

If exist then from TABLE 1 column called "identifier" check record names/values in rows such as quantity, color, discount, price1, price2 and copy content column values to TABLE 2 columns (names related - finded in rows of TABLE 1 column identifier.)

To simplify...Check ID from TABLE 1. If ID is good use identifier value and copy CONTENT column value from TABLE 1 to related ID and column name in TABLE 2.

TABLE 2

GMB :

You can pivot the source EAV table in a subquery using conditional aggregation, then join it with the target table for update. coalesce() can be used to handle missig attributes in the source table.

update table2 t2
inner join (
    select 
        post_id,
        max(case when identifier = 'quantity' then content end) quantity,
        max(case when identifier = 'color' then content end) color,
        max(case when identifier = 'discount' then content end) discount,
        max(case when identifier = 'price1' then content end) price1,
        max(case when identifier = 'price2' then content end) price2
    from table1 
    group by post_id
) t1 on t1.post_id = t2.id
set 
    t2.quantity = coalesce(t1.quantity, t2.quantity),
    t2.color    = coalesce(t1.color,    t2.color)
    t2.discount = coalesce(t1.discount, t2.discount)
    t2.price1   = coalesce(t1.price1,   t2.price1)
    t2.price2   = coalesce(t1.price2,   t2.price2)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Match row names and column names to values in another data frame

Match column and row names to column and values in another data frame

Check Column By another Column and Table

Filter row values based on if the column related to this row is existent in another table

Select Column as Column names of values in another table in SQL SERVER

SQL query: using data in a column as column names in another table?

Check names in excel row is same as table column names

How to copy values from table for another column

MYSQL Query matching column names to rows in another table

PHP MySQL: Display all column names from a given table and the values for any single given row

copy one row from a table to another and insert value of one column in same query

query to get column values based on values of another table i.e,one table values having as field names in another table

Mysql dynamic row values as column names

Inputing column values in one table from another related table

Mysql check if column in a row is contained in another column?

Copy table values to another table with + column

How to check all the values in IN clause of query exist in column of table or not

copy values of two column of a table into a column from another table

How to copy the column id from another table?

Populate table column by relative row values of another column in R

SQL: Need to check columns for values that exist in another column

[MYSQL]: Using SELECT statements to query occurrences of a unique key that exist in another column in the same Table

R - Insert values from dataframe in row of another dataframe with column names as ID

How to check if a row does not exist in another column?

Google Sheets: Check if any values exist in another column for each row

how to copy a column with another column related

Check if each column values exist in another dataframe column where another column value is the column header

Merge two columns by mapping column names to row values of another column

mysql insert into select join - copy values from one column to another table, passing through a connecting table