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中,如何为每个组选择前2行 2021-01-01
- SQL 临时表问题 2022-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 更改自动增量起始编号? 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01