merge into dest_table t
using (select coalesce(t.id,s.id)
,decode(s.id,null,t.val1,s.val1) val1
,decode(s.id,null,t.val2,s.val2) val2
....
,decode(s.id,null,t.valN,s.valN) valN
,decode(s.id,null,1,0) deleted
from dest_table t
full join src_tmp_table s on t.id = s.id
) s
on (t.id = s.id)
when matched then update
set t.val1 = s.val1
,t.val2 = s.val2
...
,t.valN = s.valN
,t.deleted = s.deleted
when not matched then insert values (
s.id
,s.val1
,s.val2
...
,s.valN
,s.deleted
)
select
names.name as name,
home.phone as home,
mobile.phone as mobile
from (select distinct name from tablename) names
left join tablename mobile on names.name = mobile.name and mobile.phonetype = 'mobile'
left join tablename home on names.name = home.name and home.phonetype = 'home'
Во-первых, как может быть два primary key?
сколько не искал подвоха, не нашел:
SELECT `object_id` FROM `object_option` WHERE `option_id` IN (2,3) GROUP BY `object_id` HAVING COUNT(*) = 2