将一个表与另一表中的许多行连接起来

发布于 2024-12-16 12:57:17 字数 1153 浏览 1 评论 0原文

我有一个本地化表,其中存储 MySQL 数据库的所有本地化内容。我正在尝试编写一个查询,对于具有多个列的给定表,该查询将检索这些列的所有本地化内容。用一个例子可能更容易理解:

foo

code    title    localized_title_code    description    localized_description_code
A001    Bar1     foo_A001_title          Baz1           foo_A001_description
A002    Bar2     foo_A002_title          Baz2           foo_A002_description

本地化

id    code                  value    locale
1     foo_A001_title        Bar1o    es
2     foo_A002_title        Bar2o    es
3     foo_A001_description  Baz1o    es
4     foo_A002_description  Baz2o    es

我想要这样的东西:

code title localized_title description localized_description
A001 Bar1  Bar1o           Baz1        Baz1o
A002 Bar2  Bar2o           Baz2        Baz2o

我知道我可以通过两个查询轻松地得到这个,例如:

select foo.title,//Or description
    localization.value as localized_title
from foo
join localization
where localization.code = concat('foo', foo.code, 'title')
and localization.locale = 'es';

但是有没有办法将两个本地化列放入一个结果中?可以安全地假设不会丢失条目(即,如果有标题,就有描述)。

I have a localized table where I store all the localized content for my MySQL database. I am trying to write a query that will retrieve, for a given table with multiple columns, all of the localized content for those columns. Probably easier to understand with an example:

foo

code    title    localized_title_code    description    localized_description_code
A001    Bar1     foo_A001_title          Baz1           foo_A001_description
A002    Bar2     foo_A002_title          Baz2           foo_A002_description

localization

id    code                  value    locale
1     foo_A001_title        Bar1o    es
2     foo_A002_title        Bar2o    es
3     foo_A001_description  Baz1o    es
4     foo_A002_description  Baz2o    es

I want something like this:

code title localized_title description localized_description
A001 Bar1  Bar1o           Baz1        Baz1o
A002 Bar2  Bar2o           Baz2        Baz2o

I know I can easily get this with two queries with something like:

select foo.title,//Or description
    localization.value as localized_title
from foo
join localization
where localization.code = concat('foo', foo.code, 'title')
and localization.locale = 'es';

but is there a way to get both of the localized columns into one result? It is safe to assume that there will be no missing entries (ie if there is a title, there is a description).

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

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

发布评论

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

评论(1

生死何惧 2024-12-23 12:57:17

这可能对你有用:

select foo.code,
    foo.title,
    title.value as localized_title,
    foo.description,
    description.value as localized_description
from foo
join localization as title on title.code = concat('foo', foo.code, 'title')
    and title.locale = 'es'
join localization as description on description.code = concat('foo', foo.code, 'description')
    and description.locale = 'es'

This may work for you:

select foo.code,
    foo.title,
    title.value as localized_title,
    foo.description,
    description.value as localized_description
from foo
join localization as title on title.code = concat('foo', foo.code, 'title')
    and title.locale = 'es'
join localization as description on description.code = concat('foo', foo.code, 'description')
    and description.locale = 'es'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文