这是我为postgresql编写的代码类型的一个非常具体的问题,我正在迁移到mysql以满足项目需求。
到目前为止在mysql中编写的代码如下:
(select substring(dt,1,9) as dt,concat(vish,visl,visn) as vis,ip
from assignment_walmart.b
where service='ss' and ua not like '%ktxn%'
and ua not like '%khte%'
and ua not like '%keynote%'
group by 1,2,3
) as A1
left join // This is where it shows the error.
(select ip,flag from
assignment_walmart.b1
group by 1,2
) as A2
on A1.ip=A2.ip
where A2.flag is NULL
group by 1,2;
错误在两个选定表的命名附近弹出“A1”和“A2”,所以我假设在mysql中不允许这样做。
请您帮我解决上述代码的替代语法,因为我必须以这种方式使用这两个表才能加入以获得所需的结果。
我究竟如何使用别名或以明显在postgresql中工作的方式连接2个表?
任何帮助都将不胜感激。
答案 0 :(得分:3)
您有一个子查询加入另一个查询。这在任一数据库中都不起作用。您需要将它们包含在select
或类似的内容中:
select A2.dt, A2.vis, count(*)
from (select substring(dt,1,9) as dt, concat(vish,visl,visn) as vis,ip
from assignment_walmart.b
where service='ss' and ua not like '%ktxn%'
and ua not like '%khte%'
and ua not like '%keynote%'
group by substring(dt,1,9), concat(vish,visl,visn), ip
) as A1 left join // This is where it shows the error.
(select ip,flag from
assignment_walmart.b1
group by ip, flag
) as A2
on A1.ip=A2.ip
where A2.flag is NULL
group by A2.dt, A2.vis;
我猜测你想要外部查询的内容以及聚合字段是什么。明确哪些字段正在聚合是个好主意。
答案 1 :(得分:0)
看起来你错过了外部查询的SELECT ... FROM
。
您认为您的查询的形式为:
SELECT ...
FROM ( inline view query ) A1
LEFT
JOIN ( inline view query ) A2
ON A1.col = A2.col ...
WHERE ...
GROUP BY ...