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 给出重复的行


基础教程推荐
- 带有WHERE子句的LAG()函数 2022-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 带更新的 sqlite CTE 2022-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01