简体   繁体   中英

SELECT table with LEFT JOIN in where clause result

Below query return : #1054 - Unknown column 'word.id' in 'where clause'

select * from word,(
SELECT sqrt(variance(ifnull(re_freq,0)))/avg(ifnull(re_freq,0)) cv
FROM pre_cat 
left join (select * from goal where wordid= word.id)g on pre_cat.id= g.catid) gg

How can I fix it?

below query return correct result but just for one record!

SELECT  avg(ifnull(re_freq,0)) , variance(ifnull(re_freq,0)),sqrt(variance(ifnull(re_freq,0)))/avg(ifnull(re_freq,0))
FROM pre_cat 
left join (select * from goal where wordid= 9690)g on pre_cat.id= g.catid

and crrect result of this query is: 0.500000000375 Upldated schema SQL fiddle http://sqlfiddle.com/#!2/de6c0/2

How cant I do it for All result? Please help me!

below stored procedure work correctly, but takes too long!

BEGIN
    DECLARE wo_id INT;
    DECLARE num INT;
    DECLARE i INT;
    DECLARE res DOUBLE;
    SELECT COUNT(*) FROM word INTO num;
    SET i=0;
    WHILE i<num DO 
        SELECT  sqrt(variance(ifnull(re_freq,0)))/avg(ifnull(re_freq,0)) FROM pre_cat left join (select * from goal where wordid= i )g on pre_cat.id= g.catid INTO res;
        update word set cv=res where id=i;
        SET i = i + 1;
    END WHILE;
END

your join syntax is wrong. as Jens said we cannot refer the outer table into inner sql. you can select like the following

select * from word,(
SELECT sqrt(variance(ifnull(re_freq,0)))/avg(ifnull(re_freq,0)) cv
FROM pre_cat 
left join (select goal.* from goal join word on goal.wordid= word.id)g on pre_cat.id= g.catid) gg
select * from word join
(SELECT sqrt(variance(ifnull(re_freq,0)))/avg(ifnull(re_freq,0)) cv,
       g.wordid
FROM pre_cat 
left join goal g on pre_cat.id= g.catid
group by g.catid) gg on gg.wordid= word.id

Just join the word table with the subquery by wordid

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