简体   繁体   中英

how to put where clause variable in the 1st layer of select query

I'm getting out of option in my query in putting select variable in the nearest query layer.

Getting this error unknown column h.date from this line gb.startTime = h.date

I know the cause, since I put the h.date inside the left join select. I think this is something wrong with the arrangement.

Does anyone have an idea?

this is my query:

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
...

You can use outside data query in just one level back subquery, not more, for example,

This is ok

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

But this is wrong

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM