mysql - Insert from same table with certain value -
i have table store meta data:
id|meta_key|meta_value ======================== 1|gender |male 2|gender |female 3|gender |female 4|gender |male 5|gender |female
now add salutation based on meta_value
becomes
id|meta_key |meta_value ======================== 1|gender |male 1|salutation|hello mr. 2|gender |female 2|salutation|hello mrs. 3|gender |female 3|salutation|hello msr. 4|gender |male 4|salutation|hello mr. 5|gender |female 5|salutation|hello mrs.
while work foreach loop i'm searching single statement mysql can magic.
you can use insert select
:
insert table_name(id, meta_key, meta_value) select id, 'salutation' meta_key, case when meta_value = 'male' 'hello mr.' else 'hello mrs.' end meta_value table_name meta_key = 'gender';
Comments
Post a Comment