将参数插入Where子句

时间:2014-11-08 02:50:45

标签: mysql where

我有这个查询,我希望从我的数据库中的数据获得排名

set @urut:=0;
set @rankhrg:=0;
select @urut:=@urut+1 as urut, a.id_tender, b.nama_tender, b.nomor_tender, b.tgl_close1 as tgl_close,
(SELECT rankhrg 
from (select sum(tot_harga) as hrg_twr, id_rekanan, id_tender, @rankhrg:=@rankhrg+1 as rankhrg from tb_real_barang where id_tender = s.id_tender  group by id_rekanan) as rank_harga
left join tb_master_tender s on s.id_tender = b.id_tender
where rank_harga.id_rekanan = a.id_rekanan
order by rank_harga.hrg_twr asc) as ranking
                                        from tb_real_tender a
                                        left join tb_master_tender b on a.id_tender = b.id_tender
                                        where a.id_rekanan = 1
                                        order by convert(a.id_tender,unsigned) desc

当我想获得rankhrg时,我想将id_tender传递给select中的select:

select sum(tot_harga) as hrg_twr, id_rekanan, id_tender, 
       @rankhrg:=@rankhrg+1 as rankhrg 
from tb_real_barang 
where id_tender = s.id_tender  
group by id_rekanan

但我总是得到错误,说s.id_tender在where子句中是未知的。

有人可以指导我如何将参数传递到该插入内容吗? 谢谢你:))

1 个答案:

答案 0 :(得分:1)

您没有加入该表tb_master_tender,也没有出现在外部查询FROM子句中。因此,您需要为内部查询单独执行JOIN,如下所示

select sum(trb.tot_harga) as hrg_twr, 
trb.id_rekanan, 
trb.id_tender, 
@rankhrg:=@rankhrg+1 as rankhrg 
from tb_real_barang trb
left join tb_master_tender s on trb.id_tender = s.id_tender  
group by trb.id_rekanan