关联Payment与Users表查询时数据列重复问题求助
解决SQL查询列重复冗余问题
你的问题出在select *的使用上:select *会同时返回Payment表的全部28列、Users表的所有列,再加上额外的users.Userid,这就导致了大量冗余重复的列。你只需要Users表的Name字段,修改查询语句、明确指定需要的列即可解决:
方案1:保留Payment所有列,仅添加Users.Name
select payment.*, users.Name from payment left join users on users.Userid = payment.CreatedBy where payment.PatientInvoiceID = ${id} and payment.DeletedBy is null
用payment.*仅获取Payment表的全部列,再单独指定users.Name,这样就不会包含Users表的其他冗余列,解决重复列问题。
方案2(更推荐):明确列出所有需要的列
如果不需要Payment的全部28列,建议直接写出需要的列名,避免不必要的数据返回,同时让SQL逻辑更清晰:
select payment.PatientInvoiceID, payment.CreatedBy, -- 补充你实际需要的其他Payment列 users.Name from payment left join users on users.Userid = payment.CreatedBy where payment.PatientInvoiceID = ${id} and payment.DeletedBy is null
内容的提问来源于stack exchange,提问作者crackerc0de
相关产品推荐
相关产品推荐

