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


基础教程推荐
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01