我有两张桌子
table1
id userid date_t game hdigit inside
1 yui 0909 alpha 8 700
2 loi 0809 beta 7 600
table2
id userid date_t game hdigit outside
1 yui 0909 alpha 8 600
2 ron 0809 gama 5 600
I want result like this
id userid date_t game hdigit inside outside
1 yui 0909 alpha 8 700 600
2 loi 0809 beta 7 600 null
3 ron 0809 gama 5 null 600
我尝试过简单的连接
select table1.userid
, table1.date_t
, table1.game
, table1.inside
, table2.outside
from table1
, table2
where table1.hdigit = table2.hdigit
AND table1.date_t = table2.date_t
AND table2.game = table1.game
AND table1.userid = table2.userid
显然,它给了我简单的共同价值观,但是如何实现上述任何帮助都会非常感激。
答案 0 :(得分:1)
出于您的目的,我认为group by
/ union all
是最佳方法:
select (@rn := @rn + 1) as id, userid, date_t, game, hdigit,
max(inside) as inside, max(outside) as outside
from ((select userid, date_t, game, hdigit, inside, null as outside
from table1
) union all
(select userid, date_t, game, hdigit, null, outside
from table2
)
) t cross join
(select @rn := 0) params
group by userid, date_t, game, hdigit;
您不希望full join
的原因是因为您似乎想要一行用于四个组合键。如果需要一行,group by
通常是可行的解决方案。