如何将where子句变量放在选择查询的第一层

时间:2019-04-10 18:23:10

标签: mysql sql

在将select变量放在最近的查询层中时,我在查询中失去了选择。

从此行unknown column h.date中获取此错误gb.startTime = h.date

我知道了原因,因为我将h.date放在了左联接选择中。我认为这种安排有问题。

有人有主意吗?

这是我的查询

set @maxBat = 10000;
set @minBat = 0;
select
    h.date,
    (select 
        count(*)
    from(
        select 
            distinct d.id, 
            d.reg,
            ifnull(bc.battleCount, 0) AS battles
        from (
            select 
                ds.id as id,
                ds.reg as reg
            ...
            ) as d
        left join
            (
            select 
                count(gb.id) as battleCount,
                gb.playerID
            from g_battles as gb
            where
                gb.startTime = h.date
            group by gb.playerID
            ) as bc on bc.playerID = d.id
        having battles between @minBat and @maxBat
        ) as e
            where e.reg = h.date
    ) as regbattle
from
    sessiontable as h
where
...

1 个答案:

答案 0 :(得分:1)

例如,您只能在一级后退子查询中使用外部数据查询,例如,

没关系

select id,(select count(index_) 
from (select index_ from log_sops) as t where t.index_ = h.version) 
from data_sops as h where id = 4

但这是错误的

select id,(select count(index_) 
from (select index_ from log_sops where id = h.version) 
as t) 
from data_sops as h where id = 4