mysql - GROUP BY in SUBQUERY WITH UNION ALL -
mysql - GROUP BY in SUBQUERY WITH UNION ALL -
i have 2 tables, 1 showing search queries history, other showing locations.
the first table search queries: searchterm | date | userid | historyid sec locations: userid | date | historyid | lat | lng
i using union bring these in sub query bring together left & bring together right create total join.
select st, fa (select webhistory.searchterm st, locationhistory.fulladdress fa webhistory left outer bring together locationhistory on webhistory.historyid=locationhistory.historyid union select webhistory.searchterm st, locationhistory.fulladdress fa webhistory right outer bring together locationhistory on webhistory.historyid=locationhistory.historyid) t
every thing working beautifully i'm getting results entries in webhistory table, , same locationhistory table. issue have when i'm getting rows have been added both. show twice!
i can't utilize union bring together more want to. if 2 of same searches added, instead of showing 2 bring together them one. need utilize union all.
i have utilize union i'm joining left , right , in mysql total bring together isn't supported.
so i'm assuming need grouping historyid id same on rows added @ same time (when search made query , location). problem is, i've tried , tested can't seem work. missing something?
lastly, while i'm asking question , because i've not yet got point of testing order date
on outer or subqueries want show recent first result?
/* more info */
i need to output:
searchterm if there no fulladdress. total address if there no searchterm. searchterm & fulladdress if there both.
currently outputs first 2 fine, latter output twice.
as far understand, want duplicates same historyid eliminated, should able utilize union
long historyid included in union
duplicates another historyid won't eliminated;
select st, fa (select webhistory.searchterm st, locationhistory.fulladdress fa, webhistory.historyid webhistory left outer bring together locationhistory on webhistory.historyid=locationhistory.historyid union select webhistory.searchterm st, locationhistory.fulladdress fa, locationhistory.historyid webhistory right outer bring together locationhistory on webhistory.historyid=locationhistory.historyid) t
mysql subquery union-all
Comments
Post a Comment