I have been searching for an answer to this problem and cant find one
I am using my sql with Drupal on a LAMP stack
first, if this is better in a cron job then let me know
My problem
I have a form module that I must use. It saves the data like:
ID, element_id, group_id, element_value
1 0 1 Name
2 1 1 Address
3 2 1 DOB etc...
I want to be able to take the element values and put them into 1 row in another table
so I have currently
INSERT INTO new_table (name, street_address,street_address_line_2, city, state, zip,
country, dob)
SELECT element_value
FROM submited_table
WHERE group_id = 'some_group_id'
I am stuck as to how to place the different values in the same row to make a single record.
Can anyone help with with this?
One way to do this is through aggregation:
INSERT INTO new_table (name, address, dob)
SELECT max(case when element_id = 1 then element_value end) as name,
max(case when element_id = 2 then element_value end) as address,
max(case when element_id = 3 then element_value end) as DOB,
. . .
FROM submited_table
WHERE group_id = 'some_group_id';
This assumes that element_id
is identifying what each row in the first table refers to. It also assumes that each element value corresponds to a column in the target table.
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.