我有两个表具有相同的列。我想获取两个表中值不同的列。如何实现这一点?请帮助。我卡住了。
imageid是两个表中的主键。第一个表中的图像元素不一定存在于第二个表中。
first table
imageid name id
1 priya 001
2 neha 002
3 divya 003
4 santo 004
second table
imageid name id
1 priy 001
2 neha 003
4 santo 004
Result
imageid firstdata seconddata columnname
1 priy priya name
2 002 003 id
我试过这个 -
select t1.imageid, t1.name as firstdata, t2.name as seconddata, 'name' as colname
from t1 join
t2
on t1.imageid = t2.imageid
where t1.name <> t2.name
union all
select t1.imageid, t1.id as firstdata, t2.id as seconddata, 'id'
from t1 join
t2
on t1.imageid = t2.imageid
where t1.id <> t2.id;
实际上我有136列,我希望查询得到优化。