【发布时间】: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 语句。