如何解决这个不同的SQL查询

时间:2014-06-11 05:54:56

标签: php sql

select DISTINCT IDDetail, NamaProperti, Provinsi, Kota, Area, TipeListing, JenisProperti, Hadapnya, JenisSertifikat, Harga, LT, LB, foto1 from MsProperti as mp, MsSertifikat as ms, MsDetail as md, MsTipeListing mt where md.IDProperti = mp.IDProperti and md.IDSertifikat = ms.IDSertifikat and md.IDTipeListing = mt.IDTipeListing and Provinsi = 'BSD' or Kota = 'BSD' or Area = 'BSD' and AlreadySold = 0 order by IDDetail desc

我知道没有明显它会给我很多相同的数据,然后我在Select之后放DISTINCT使它只给我1个相同的数据。但它没有什么不同。

有什么建议吗?

答案是在WHERE查询结束后添加GROUP BY。 这是asnwer

    select IDDetail, NamaProperti, Provinsi, Kota, Area, TipeListing, JenisProperti, Hadapnya, JenisSertifikat, Harga, LT, LB, foto1
from MsProperti as mp, MsSertifikat as ms, MsDetail as md, MsTipeListing mt
where md.IDProperti = mp.IDProperti and 
md.IDSertifikat = ms.IDSertifikat and 
md.IDTipeListing = mt.IDTipeListing ".$tambahquery." and AlreadySold = 0
group by IDDetail
order by IDDetail desc

感谢。

1 个答案:

答案 0 :(得分:0)

使用GROUP BY代替DISTINCT。像:

SELECT 
    IDDetail, 
    NamaProperti, 
    Provinsi, 
    Kota, 
    Area, 
    TipeListing, 
    JenisProperti, 
    Hadapnya, 
    JenisSertifikat, 
    Harga, 
    LT, 
    LB, 
    foto1 
FROM 
    MsProperti as mp, 
    MsSertifikat as ms, 
    MsDetail as md, 
    MsTipeListing mt 
WHERE 
    md.IDProperti = mp.IDProperti AND 
    md.IDSertifikat = ms.IDSertifikat AND 
    md.IDTipeListing = mt.IDTipeListing AND 
    Provinsi = 'BSD' OR 
    Kota = 'BSD' OR 
    Area = 'BSD' AND 
    AlreadySold = 0 
GROUP BY
    IDDetail, 
    NamaProperti, 
    Provinsi, 
    Kota, 
    Area, 
    TipeListing, 
    JenisProperti, 
    Hadapnya, 
    JenisSertifikat, 
    Harga, 
    LT, 
    LB, 
    foto1 
ORDER BY 
    IDDetail DESC