如何防止此MySQL连接中出现重复结果?
我试图显示特定用户的费用列表 每个费用都可以有一个类型 费用、用户和类型都在不同的表中 当我尝试使用JOIN显示列表时,会得到重复的值:如何防止此MySQL连接中出现重复结果?,mysql,join,Mysql,Join,我试图显示特定用户的费用列表 每个费用都可以有一个类型 费用、用户和类型都在不同的表中 当我尝试使用JOIN显示列表时,会得到重复的值: SELECT DISTINCT gastos.gastoID, gastos.userID, tiposPago.tipoPago AS tipo FROM gastos LEFT JOIN tiposPagoGastos ON gastos.userID = tipos
SELECT DISTINCT
gastos.gastoID,
gastos.userID,
tiposPago.tipoPago AS tipo
FROM gastos
LEFT JOIN tiposPagoGastos
ON gastos.userID = tiposPagoGastos.userID
LEFT JOIN tiposPago
ON tiposPagoGastos.tiposPagoID = tiposPago.tiposPagoID
WHERE gastos.userID = 8
这是我的费用表:
CREATE TABLE gastos(
gastoID int unsigned not null auto_increment primary key,
userID int not null,
fecha char(25) null COMMENT 'en formato timestamp',
monto int(10) null,
detalles text null
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
这是我的付款类型表
CREATE TABLE tiposPago(
tiposPagoID int unsigned not null auto_increment primary key,
tipoPago varchar(300) null,
userID int not null,
detalles text null
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
这是联接付款类型和费用的表
CREATE TABLE tiposPagoGastos(
tiposPagoGastosID int unsigned not null auto_increment primary key,
gastoID int not null,
tiposPagoID int not null,
userID int not null
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
此用户有两种类型的费用:“美国运通”和“Visa”
这是我期望的结果:
gastoID -- userID -- tipo
64 ------- 8 ------ American Express
65 ------- 8 ------ Visa
SELECT DISTINCT
gastos.gastoID,
gastos.userID,
tiposPagoGastos.tiposPagoID,
tiposPago.tipoPago
FROM gastos
LEFT JOIN tiposPagoGastos
ON gastos.gastoID = tiposPagoGastos.gastoID
LEFT JOIN tiposPago
ON tiposPagoGastos.tiposPagoID = tiposPago.tiposPagoID
WHERE gastos.userID = 8
这是我得到的结果:
gastoID -- userID -- tipo
64 ------- 8 ------ American Express
65 ------- 8 ------ American Express
64 ------- 8 ------ Visa
65 ------- 8 ------ Visa
gastoID -- userID -- tipo
64 ------- 8 ------ American Express
65 ------- 8 ------ American Express
如果我向查询中添加:groupbygastos.gastoID
,则得到的结果如下:
gastoID -- userID -- tipo
64 ------- 8 ------ American Express
65 ------- 8 ------ American Express
64 ------- 8 ------ Visa
65 ------- 8 ------ Visa
gastoID -- userID -- tipo
64 ------- 8 ------ American Express
65 ------- 8 ------ American Express
最后一个结果不正确,因为胃部“65”应该是VISA。
输入信息如下:
tiposPago表格
tiposPagoID tipoPago userID detalles
1 VISA 8 banco Galicia
2 American Express 8 Santander
gastoID userID tiposPagoID
64 8 2
65 8 1
tiposPagoGastos表
tiposPagoID tipoPago userID detalles
1 VISA 8 banco Galicia
2 American Express 8 Santander
gastoID userID tiposPagoID
64 8 2
65 8 1
如何在没有重复结果的情况下获得正确的列表?这起作用了,我使用了错误的字段进行连接(以防万一,有朝一日,我会发布答案):
gastoID -- userID -- tipo
64 ------- 8 ------ American Express
65 ------- 8 ------ Visa
SELECT DISTINCT
gastos.gastoID,
gastos.userID,
tiposPagoGastos.tiposPagoID,
tiposPago.tipoPago
FROM gastos
LEFT JOIN tiposPagoGastos
ON gastos.gastoID = tiposPagoGastos.gastoID
LEFT JOIN tiposPago
ON tiposPagoGastos.tiposPagoID = tiposPago.tiposPagoID
WHERE gastos.userID = 8
在当前结果中,没有重复项。两排的胃壁是不同的。您需要显示预期结果所基于的示例输入数据。将“左联接”更改为“内部联接”JOIN@GurwinderSingh我更新了这个问题来澄清它。我拥有的示例数据就是我显示的数据(其他结果我已经删除了它们):gastoID 64应该是美国运通类型,gastoID 65应该是Visa,显示的结果不正确。@PeterKalef'DidiSoft我仍然使用内部联接得到错误的结果。@Rosamunda-给出的是预期结果和实际结果,而不是输入数据。