我有一个联系人和语言表,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
我想只显示一个数据,任何人都可以告诉我如何做到这一点,谢谢
答案 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 *
。