简体   繁体   English

选择查询中的重复行

[英]duplicated rows in select query

I am trying to run a query to get all the customers from my database.我正在尝试运行查询以从我的数据库中获取所有customers These are my tables in a diagram :这些是我在图表中的表格:

在此处输入图片说明

when running the query by joining the table Companies_Customers and the Customers table based on the customerId in both tables(doesn't show in the join table in the pic), I get duplicate rows, which is not the desired outcome.当通过连接表 Companies_Customers 和基于两个表中的 customerId 的客户表运行查询时(未显示在图片的连接表中),我得到重复的行,这不是所需的结果。 This is normal from a database standpoint since a Customer can be related to different companies (Companies can share single customer).从数据库的角度来看,这是正常的,因为客户可以与不同的公司相关(公司可以共享单个客户)。

My question is how do I get rid of the duplication via SQL.我的问题是如何通过 SQL 消除重复。

There can be 2 approaches to your problem.您的问题可以有 2 种方法。

Either only select data from Customers table:仅从客户表中选择数据:

SELECT * FROM Customers

Or select from both tables joined together, but without CompanyName and with GROUP BY CompanyCustomerId - although I highly suggest the first approach.或者从连接在一起的两个表中选择,但没有 CompanyName 和GROUP BY CompanyCustomerId - 尽管我强烈建议第一种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM