如何显示明显的价​​值

时间:2014-06-02 09:30:00

标签: mysql sql

我有一个联系人和语言表,contact表格,下面的列也是

   id   Contact group   Media   E-mail                  Nationality        Country                      City      Area  Gender  Married Children    Drivers license

   107     Contactgroup1    SMS     nasir.kv01@gmail.com       India           United Arab Emirates         Dubai             Male     No       
   108     Contactgroup1    SMS     abdulrahim922@gmail.com    Pakistan        United Arab Emirates         Dubai             Male     Yes  

用于language表,如下所示

id  language
1   English
2   Hindi

我也使用帮助表名contactlanguage,将联系人ID与语言ID匹配,就像这样

contactid   languageid
   107         1
   108         1
   107         2

我在搜索功能中运行此查询,

SELECT DISTINCT *
FROM contact
INNER JOIN contactlanguage ON contact.id = contactlanguage.contactid
INNER JOIN language ON contactlanguage.languageid = language.id
WHERE isdeleted = 0 AND language.language in ('English', 'Hindi', '')

相同的数据像这样重复

id   Contact group     Media    E-mail                  Nationality        Country                      City      Area  Gender  Married Children    Drivers license

107     Contactgroup1   SMS     nasir.kv01@gmail.com       India           United Arab Emirates         Dubai             Male     No   

107     Contactgroup1   SMS     nasir.kv01@gmail.com       India           United Arab Emirates         Dubai             Male     No   

我想只显示一个数据,任何人都可以告诉我如何做到这一点,谢谢

1 个答案:

答案 0 :(得分:4)

如果您只显示联系人表格中的列而不是SELECT *,那么您将避免因具有多种语言的联系人造成重复。

SELECT DISTINCT
        contact.id,
        contact.`Contact group`,
        contact.Media,
        contact.E-mail,
        contact.Nationality,
        contact.Country,
        contact.City,
        contact.Area,
        contact.Gender,
        contact.Married,
        contact.Children,
        contact.`Drivers license`
FROM contact
INNER JOIN contactlanguage ON contact.id = contactlanguage.contactid
INNER JOIN language ON contactlanguage.languageid = language.id
WHERE isdeleted = 0 AND language.language in ('English', 'Hindi', '');

对于它的价值,您应该明确列出您想要的列。生产代码中的It is not good practice to use SELECT *