如何避免SQL中的重复数据 [英] How to avoid duplicate data in SQL

查看:70
本文介绍了如何避免SQL中的重复数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我有4个不同的表,我通过使用外键从中恢复值不同的值。但问题是因为外键数据重复传入意味着它会传入相同的数据。



我尝试过的事情:



i也尝试使用distinct关键字以避免重复值,但它对我不起作用

查询是



选择users.Name,users.UserID,users.Contact,users.photos,pay.PaymentID,pay.Payble,pay.Discount,pay.Total_paid,pay.Balance_Remain,pay.Payment_Mode,pay.Payment_Type ,pay.Payment_Describtion,pack.PackageID,pack.Package_Name,pack.Package_Duration,pack.Package_Amount,sub.SubscriptionID,sub.Package_Expire_Date,sub.Package_StartDate from tblUser as users

inner join tblPayment as pay <用户上的
.UserID = pay.UserId

内部联接tblPackages as pack

on pack.PackageID = pay.PackageId

inner join tblSubscription as sub

on users.UserID = sub.UID

解决方案

通常在加入来自加入的数据表格是重复的,但是这样的方式是相同的组合只出现一次(取决于连接类型)。查询执行的原因是设计,但总体结果会受到表中数据的影响。



了解联接的一个好方法是看一下 SQL连接的可视化表示 [ ^ ]


你好



如果你的数据有重复数据表,最好将其分为2个表。这称为数据库规范化。一个有用的教程是:

StudytoNight



例如,如果你有FieldName:城市重复1000人,你最好分开桌子



最好的问候

hello i have 4 differenct table from which i am retriving the value different value by using foreign key. but problem is that because of foreign key data is comming in duplicate mean it is coming same data.

What I have tried:

i have also try distinct keyword for avoiding duplicate value but it is not work for me
query is

select users.Name,users.UserID,users.Contact,users.photos,pay.PaymentID,pay.Payble,pay.Discount,pay.Total_paid,pay.Balance_Remain,pay.Payment_Mode,pay.Payment_Type,pay.Payment_Describtion,pack.PackageID,pack.Package_Name,pack.Package_Duration,pack.Package_Amount,sub.SubscriptionID,sub.Package_Expire_Date,sub.Package_StartDate from tblUser as users
inner join tblPayment as pay
on users.UserID=pay.UserId
inner join tblPackages as pack
on pack.PackageID=pay.PackageId
inner join tblSubscription as sub
on users.UserID=sub.UID

解决方案

Typically in join the data from the joined tables is duplicated but in such way that the same combination is present only once (depending on the join type). What comes to query execution this is by design but however, the overall result is affected by the data you have in the tables.

One good way to understand the joins is having a look at Visual Representation of SQL Joins[^]


Hello

if you have duplicated data in your table, it is better to divide it into 2 tables. This is called database normalization. a useful tutorial is :
StudytoNight.

for example if you have FieldName : City duplicated for 1000 person, you would better to divide the table

best regards


这篇关于如何避免SQL中的重复数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆