sql:在另一个表中显示带有条件的数据

时间:2013-06-29 21:01:45

标签: sql database exists

我有这样的架构

http://sqlfiddle.com/#!3/690e8

我想显示TransactionID和TransactionDate(格式' yyyy.MM.dd'),其中MedicineTypeName不是' Syrup'和MedicinePrice大于15000使用转换,存在和...

结果应该是这样的

result pics

我试过了:

select th.TransactionID,convert(varchar,th.TransactionDate,20)[tanggal]
from TransactionHeader th 
join TransactionDetail td on th.TransactionID = td.TransactionID 
where td.MedicineID  in 
(
select md.MedicineID
from MsMedicine md join MsMedicineType mmt on mmt.MedicineTypeID = d.MedicineTypeID
where mmt.MedicineTypeName not like 'syrup' and md.MedicinePrice > 15000

)

1 个答案:

答案 0 :(得分:0)

将d.MedicineTypeID更改为md.MedicineTypeID

添加了distinct以防止重复

select distinct th.TransactionID,convert(varchar,th.TransactionDate,20)[tanggal]
from TransactionHeader th 
join TransactionDetail td on th.TransactionID = td.TransactionID 
where td.MedicineID  in 
(
select md.MedicineID
from MsMedicine md join MsMedicineType mmt on mmt.MedicineTypeID = md.MedicineTypeID
where mmt.MedicineTypeName not like 'syrup' and md.MedicinePrice > 15000

)

sql fiddle