SQL 列合并和聚合函数

发布于 2024-07-22 08:16:46 字数 508 浏览 3 评论 0原文

我有一个简单的表,有两列(两列感兴趣,加上一个ID)...我有一个ajax搜索,它只是寻找关键字...然后将其发送到我真正的搜索...ajax搜索不关心它们是什么,但它们需要不同...

我怎样才能将两列合并在一起:

City, Country

Krakow, Poland
Warsaw, Poland
Austin, USA
New York, USA
Prague, Czech Republic

这样我就可以

Keyword, Sideinfo

Krakow, Poland
Warsaw, Poland
Austin, USA
Prague, Czech Republic
USA, Country (only once)
Poland, Country
Czech Republic, Country

尝试做一个 UNION 但我不确定如何做一个 WHERE LIKE 'keyword%'

希望这是有道理的......

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

[浮城] 2024-07-29 08:16:46

尝试这个。

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%'

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%'
り繁华旳梦境 2024-07-29 08:16:46

这应该可以做到:

select distinct country Keyword,'Country' SideInfo from Cities
union all
select City Keyword,Country SideInfo from Cities

This should do it:

select distinct country Keyword,'Country' SideInfo from Cities
union all
select City Keyword,Country SideInfo from Cities
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文