MySQL 左连接子选择

发布于 2024-12-06 04:43:30 字数 477 浏览 0 评论 0原文

我有一个简单的语言/模板 ID 表:

语言 |模板

en, t1
zh, t1
au, t2
ge, t3
zh, t1
ge, t2
ge, t3

模板始终是 t1、t2 或 t3。总共有 3 种语言:en、au、ge。

表中有更多信息,我只是显示与这个问题相关的内容,我将使用数据进行绘图,因此需要它以这种格式返回:en

, t1, 3
zh, t2, 0
zh, t3, 0
au, t1, 0
au, t2, 1
au, t3, 0
ge, t1, 0
ge, t2, 1
ge, t3, 2

无论每种语言中有多少个模板出现,它都会进行计数。但是,我遇到的问题是,如果表中没有该特定语言的模板 ID,则返回零计数。

我在想它需要在模板 id 上进行某种左连接子选择,以确保为每种语言返回 3 个模板 id?

I have a simple table of languages / template id's:

language | template

en, t1
en, t1
au, t2
ge, t3
en, t1
ge, t2
ge, t3

Template is always either t1,t2, or t3. In total there are 3 languages: en, au, ge.

There is lots more information in the table, I am just showing what is relevant to this question, I will be using the data for graphing and so need it returning in this format:

en, t1, 3
en, t2, 0
en, t3, 0
au, t1, 0
au, t2, 1
au, t3, 0
ge, t1, 0
ge, t2, 1
ge, t3, 2

This counts however many template occurrences there are in each language. But, the problem I have is returning a zero count if there are no template id's for that particular language in the table.

I was thinking it would need some sort of left join sub select on the template id to make sure the 3 template id's are returned for each language?

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

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

发布评论

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

评论(3

家住魔仙堡 2024-12-13 04:43:30

可能有更好的方法来做到这一点,我还没有在 MySQL 中测试它,但以下方法在 SQL Server 2005 中有效:

Select a.language, b.template, count (c.template) as combo_count
from
(select distinct language from tablename) as a
inner join (select distinct template from tablename) as b on 1 < 2 /* this could be cross join, same thing. */
left outer join tablename c on c.language = a.language and c.template = b.template
group by a.language, b.template
order by 1, 2

以下是示例数据的结果:

au  t1  0
au  t2  1
au  t3  0
en  t1  3
en  t2  0
en  t3  0
ge  t1  0
ge  t2  1
ge  t3  2

There might be a better way of doing this, and I haven't tested it in MySQL, but the following works in SQL Server 2005:

Select a.language, b.template, count (c.template) as combo_count
from
(select distinct language from tablename) as a
inner join (select distinct template from tablename) as b on 1 < 2 /* this could be cross join, same thing. */
left outer join tablename c on c.language = a.language and c.template = b.template
group by a.language, b.template
order by 1, 2

Here are the results with your sample data:

au  t1  0
au  t2  1
au  t3  0
en  t1  3
en  t2  0
en  t3  0
ge  t1  0
ge  t2  1
ge  t3  2
做个ˇ局外人 2024-12-13 04:43:30
  Select a.language, a.template, Count(*) count
  From (Select Distinct language, template From table) a
     Left Join table b
         On b.language = a.language
             And b.template = b.template
  Group By a.language, a.template
  Select a.language, a.template, Count(*) count
  From (Select Distinct language, template From table) a
     Left Join table b
         On b.language = a.language
             And b.template = b.template
  Group By a.language, a.template
撑一把青伞 2024-12-13 04:43:30

您需要的是两个表格,其中列出了语言和模板的可能值。

CREATE TABLE language (...) AS SELECT DISTINCT language FROM your_table;
CREATE TABLE template (...) AS SELECT DISTINCT template FROM your_table;

然后你可以做这样的事情:

SELECT l.language, t.template, SUM(CASE WHEN yours.language IS NULL THEN 0 ELSE 1 END) count
FROM language l CROSS JOIN template t
LEFT OUTER JOIN your_table yours ON l.language = yours.language AND t.template = yours.template
GROUP BY l.language, t.template;

What you need is two tables that list the possible values for language and template.

CREATE TABLE language (...) AS SELECT DISTINCT language FROM your_table;
CREATE TABLE template (...) AS SELECT DISTINCT template FROM your_table;

Then you can do something like this:

SELECT l.language, t.template, SUM(CASE WHEN yours.language IS NULL THEN 0 ELSE 1 END) count
FROM language l CROSS JOIN template t
LEFT OUTER JOIN your_table yours ON l.language = yours.language AND t.template = yours.template
GROUP BY l.language, t.template;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文