Nested Select di MySQL
Nested Selected istilahnya select dalam select di MySQL belum bisa setangguh di SQL Server.
Contoh di SQL Server :
Select kode, nama from table1 where kode IN (select kode from table2 )
Source diatas tidak bisa berjalan di MySQL. Untuk mengatasi hal ini kita memakai trik dengan mengacu aturan di MySQL
Select kode, nama from table1 where kode IN (letakkan variabel kode disini dari table2)
kalo di Delphi , saya memiliki contoh trik seperti ini :
kodemk:='';
WHILE NOT tdkrs.Eof do
begin
if tdkrs.Bof then
kodemk:=' '''+trim(tdkrsfc_kdmk.AsString)+''',' else
kodemk:=kodemk+' '''+trim(tdkrsfc_kdmk.AsString)+''',';
tdkrs.Next;
if tdkrs.Eof then
kodemk:=kodemk+''''+trim(tdkrsfc_kdmk.AsString)+'''';
end;
tmst.close;
tmst.Sql.Text:=format(memo1.Text,[kodemk]);
tmst.Open;
Dimana tmst adalah table1 dan tdkrs sebagai table2. Dan isi dari memo1.text adalah :
select a.* , rtrim(c.fv_value) as status,
concat(a.fc_kdjenjang,a.fc_kdjurusan,a.fc_kdprodi,a.fc_kurikulum,a.fc_kdmk) as fc_id
from t_mk a
left outer join t_status c on c.fc_kode=a.fc_kdstatus and c.fc_param='STSMK'
where a.fc_kdjenjang=:fc_kdjenjang and a.fc_kdjurusan=:fc_kdjurusan and
a.fc_kdprodi=:fc_kdprodi and a.fc_kurikulum=:fc_kurikulum and a.fc_kdsem='1'
and a.fc_kdmk NOT IN (%s)
order by a.fc_kurikulum, a.fc_kdsem, a.fc_kdmk
Demikian semoga bermanfaat
--Hendrik--
0 komentar:
Posting Komentar