在具有多个 INNER JOINS 的 SELECT 查询中使用 Count()。找不到正确的输出

发布于 2025-01-14 04:08:26 字数 1399 浏览 0 评论 0原文

这是我的查询:

select distinct 
    p.*,
    d.tech_id, 
    d.id, 
    prio.priority 
from
    data.curated_data s
inner join
    data.curated_details p on p.curated_scheme = s.curated_scheme 
                           and p.curated_category = s.curated_category
inner join
    data1.data_defn d on d.service_id = p.service_id
inner join 
    data1.data_priority prio on prio.ingest_data = d.ingest_data
                             and p.curated_scheme = %s
                             and s.manually_curated_flag = 'Y'
                             and s.autosuggest_kind = %s
limit %s
offset %s

这就是我试图做的:

select count(*) 
from ( select distinct p.*,
              d.tech_id, 
              d.id, 
              prio.priority 
       from data.curated_data s
       inner join data.curated_details p on p.curated_scheme = s.curated_scheme and p.curated_category = s.curated_category
       inner join data1.data_defn d on d.service_id = p.service_id
       inner join data1.data_priority prio on prio.ingest_data = d.ingest_data
       and p.curated_scheme = %s
       and s.manually_curated_flag = 'Y'
       and s.autosuggest_kind = %s
       limit %s
       offset %s
      )

但是,这会导致错误

错误代码:1248。每个派生表必须有自己的别名

为了处理这个问题,我向 count select 查询添加了别名,但随后它返回了错误的输出 1,而表中有大约 500 行输出表。

This is my query:

select distinct 
    p.*,
    d.tech_id, 
    d.id, 
    prio.priority 
from
    data.curated_data s
inner join
    data.curated_details p on p.curated_scheme = s.curated_scheme 
                           and p.curated_category = s.curated_category
inner join
    data1.data_defn d on d.service_id = p.service_id
inner join 
    data1.data_priority prio on prio.ingest_data = d.ingest_data
                             and p.curated_scheme = %s
                             and s.manually_curated_flag = 'Y'
                             and s.autosuggest_kind = %s
limit %s
offset %s

This is what I tried to do:

select count(*) 
from ( select distinct p.*,
              d.tech_id, 
              d.id, 
              prio.priority 
       from data.curated_data s
       inner join data.curated_details p on p.curated_scheme = s.curated_scheme and p.curated_category = s.curated_category
       inner join data1.data_defn d on d.service_id = p.service_id
       inner join data1.data_priority prio on prio.ingest_data = d.ingest_data
       and p.curated_scheme = %s
       and s.manually_curated_flag = 'Y'
       and s.autosuggest_kind = %s
       limit %s
       offset %s
      )

However, this leads to an error of

Error Code: 1248. Every derived table must have its own alias

To handle that I added alias to my count select query but then it returned a wrong output of 1, while the there is around 500 rows in the output table.

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

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

发布评论

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

评论(1

峩卟喜欢 2025-01-21 04:08:26

尝试在派生表|子查询的末尾使用别名

select count(*) from(select distinct p.*,d.tech_id, d.id, prio.priority from data.curated_data s
                INNER JOIN data.curated_details p on p.curated_scheme = s.curated_scheme and p.curated_category = s.curated_category
                INNER JOIN data1.data_defn d on d.service_id = p.service_id
                inner join data1.data_priority prio
                on prio.ingest_data = d.ingest_data
                and  p.curated_scheme = %s
                and s.manually_curated_flag = 'Y'
                and s.autosuggest_kind = %s
                limit %s
                offset %s) as temp  ---- here

Try an alias at the end of derived table|subquery

select count(*) from(select distinct p.*,d.tech_id, d.id, prio.priority from data.curated_data s
                INNER JOIN data.curated_details p on p.curated_scheme = s.curated_scheme and p.curated_category = s.curated_category
                INNER JOIN data1.data_defn d on d.service_id = p.service_id
                inner join data1.data_priority prio
                on prio.ingest_data = d.ingest_data
                and  p.curated_scheme = %s
                and s.manually_curated_flag = 'Y'
                and s.autosuggest_kind = %s
                limit %s
                offset %s) as temp  ---- here
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文