Mysql join gives duplicate rows(Mysql join 给出重复的行)
问题描述
我有 2 个表,我正在使用 join 从这 2 个表中获取公共记录.我使用了以下查询,但我的问题是我的记录翻了一番.查询如下
I have 2 tables and i am using join to get common records from those 2 tables. i have used the following query but my problem is i am getting the records doubled. The query is as follows
SELECT * FROM pos_metrics pm INNER JOIN pos_product_selling pps ON
pm.p_id=pps.p_id WHERE pm.p_id='0' AND pps.pos_buying_id='0' AND pm.type=1
pos_metrics 表:
pos_metrics table:
pos_product_sales 表:
pos_product_selling table:
输出:
编辑
当我尝试将 GROUP BY 和 DISTINCT 一起使用时,我没有得到重复项,但第二个表中的值重复了.还有其他解决方案吗?
EDIT
When I tried to use GROUP BY and DISTINCT together I am not getting duplicates but the value from the second table is repeated. Any other solutions ?
推荐答案
在pos_metrics表中添加一个主键并引入到pos_product_sales表中,然后做一个JOIN 基于主键以及其他条件.那样你就不会得到这些重复项了.
Add a primary key in the pos_metrics table and introduce it to the pos_product_selling table, then do a JOIN based on the primary key as well as the other criteria. You won't get these duplicates then.
这里有重复的原因是因为不可能根据值对两个表进行唯一比较.
The reason you have duplicates over here is because there is no possibility of an unique comparison to be done on both tables based on a value.
这篇关于Mysql join 给出重复的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Mysql join 给出重复的行
基础教程推荐
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 无法解决整理冲突 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
