【问题标题】:Mysql joins with complicated subqueryMysql加入复杂的子查询
【发布时间】:2017-10-10 05:50:03
【问题描述】:

我有两个 select 语句,我想通过它们的公共列加入它们:

第一个查询:

选择self_code为'DSLAM_code',count(self_code)为'Registered' from obj_port group by self_code;

输出:

+------------+------------+
| DSLAM_code | Registered |
+------------+------------+
| 10000      |        317 |
| 10001      |        344 |
| 10002      |         93 |
+------------+------------+

第二次查询:

选择 substring_index(dslamportid,":",1) 作为 DSLAM_code, count(substring_index(dslamportid,":",1)) as Active from radacct where dslamportid like '%:%' group by substring_index(dslamportid,":",1);

输出:

+------------+--------+
| DSLAM_code | Active |
+------------+--------+
| 10000      |    265 |
| 10001      |    299 |
| 10002      |     83 |
+------------+--------+

我尝试使用以下查询加入两个输出表,但未成功:

选择 A.self_code 作为 'DSLAM_code',A.count(self_code) 作为 'Registered' from obj_port A group by F.self_code left join (select substring_index(dslamportid,":",1) 作为 dslam, count(substring_index(dslamportid,":",1)) 作为来自 radacct 的计数,其中 dslamportid like '%:%' group by substring_index(dslamportid,":",1)) B 关于 A.self_code = B.dslam;

我得到语法错误:

错误代码:1064。您的 SQL 语法有错误;检查 与您的 MySQL 服务器版本相对应的手册 'left join 附近使用的语法(选择 substring_index(dslamportid,":",1) as dslam, count(substring_' at line 2

提前感谢亲爱的社区!

【问题讨论】:

  • 在 join 语句之前不能有 group by 语句。

标签: mysql join subquery


【解决方案1】:

好的,我找到了一种方法:

select a.DSLAM_code, a.Registered, b.active from (select self_code 作为'DSLAM_code',计数(self_code)作为来自obj_port组的'Registered' 通过 self_code )左连接(选择 substring_index(dslamportid,":",1) 作为 DSLAM_code, count(substring_index(dslamportid,":",1)) as Active from radacct where dslamportid like '%:%' group by substring_index(dslamportid,":",1) ) b a.dslam_code = b.dslam_code;

【讨论】:

    猜你喜欢
    • 2014-02-10
    • 1970-01-01
    • 2015-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-20
    相关资源
    最近更新 更多