SQL Column merge and aggregate functions
Question
I have a simple table with two columns (well two columns of interest, plus just an ID)... I have an ajax search which is just looking for keywords... which are then sent to my real search... the ajax search doesn't care what they are, but they need to be distinct...
How can I merge the two columns together:
City, Country
Krakow, Poland
Warsaw, Poland
Austin, USA
New York, USA
Prague, Czech Republic
So that I would get
Keyword, Sideinfo
Krakow, Poland
Warsaw, Poland
Austin, USA
Prague, Czech Republic
USA, Country (only once)
Poland, Country
Czech Republic, Country
I tried doing a UNION but I'm not sure how i'd do a WHERE LIKE 'keyword%'
Hope that makes sense...
Solution
Try this.
SELECT Keyword, SideInfo
FROM
(
SELECT
DISTINT City as Keyword, Country as SideInfo
FROM Table
UNION
SELECT
DISTINCT Country, 'Country'
FROM Table
) AS InnerQuery
Where Keyword LIKE '%blah%'
OTHER TIPS
This should do it:
select distinct country Keyword,'Country' SideInfo from Cities
union all
select City Keyword,Country SideInfo from Cities
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow