mysql - SQL - "merge" 3 tables with JOIN -
i want select users table depending 2 relation tables
the structure:
[user] uid | firstname | lastname | ... --------------------------------- 482 | usera | usera | ... 885 | userb | userb | ... 405 | userc | userc | ... 385 | userd | userd | ... [news_info] uid_local | uid_foreign -------------------------------- 125 | 482 100 | 405 [news_add] uid_local | uid_foreign -------------------------------- 125 | 885 105 | 385
now want select usera , userb via uid_local -> 125, [news_info] , [news_add]
select nnfo.uid_local, user.* user join news_info nnfo on nnfo.uid_foreign = user.uid nnfo.uid_local = 125
result = usera // works
select nadd.uid_local, user.* user join news_add nadd on nadd.uid_foreign = user.uid nadd.uid_local = 125
result = userb // works
now "merge" sql statement one...to usera , userb
select nnfo.uid_local, nadd.uid_local, user.* user join news_info nnfo on nnfo.uid_foreign = user.uid join news_add nadd on nadd.uid_foreign = user.uid nnfo.uid_local = 125 , nadd.uid_local = 125
result = empty // no errors.....
what wrong...i tryed other statements no result :(
when merging results of queries, there 2 different approaches:
- union
- joins
to know 1 choose have know whether there's technical link between tables in 2 queries wish merge. in case, there's functional link (nnfo.uid_local = 125 , nadd.uid_local = 125
) it's not enough join these 2 universes. still could result 1 select
, think final result unclear , somehow not in spirit of sql provides.
i'd go towards union solution:
select nnfo.uid_local, user.* user join news_info nnfo on nnfo.uid_foreign = user.uid nnfo.uid_local = 125 union select nadd.uid_local, user.* user join news_add nadd on nadd.uid_foreign = user.uid nadd.uid_local = 125
edit: see davek's result implements union all
instead of union
. assumed wished return usera once if happened appear in both independant queries, that's why used simple union
operator vs union all
return duplicate results well.
Comments
Post a Comment