【问题标题】:How to find the mismatch rows in sql - postgresl?如何在 sql - postgresql 中找到不匹配的行?
【发布时间】:2021-05-04 00:19:05
【问题描述】:

我有 2 张桌子。我想找到不匹配的行

Input 
Table name: sale_order

header_id      name   date_col                        total_amt
45             apple  2021-01-26 19:55:33.350589      100
32             grape  2021-01-27 19:55:33.350589      200
22             plums  2021-01-27 17:55:33.350589      250
30             lemon  2021-01-28 19:55:33.350589      400

Table name: bill_order

id   product_id   date_col                        total_amt
1    45           2021-01-26 19:55:33.350589      200
2    32           2021-01-27 19:55:33.350589      200
3    22           2021-01-27 17:55:33.350589      500
4    30           2021-01-28 19:55:33.350589      400

输出:不匹配(total_amt 中的值错误)

select  sale_order.header_id ,sale_order.total_amt,bill_order.total_amt 
FROM sale_order
INNER JOIN bill_order ON sale_order.total_amt != bill_order.total_amt` and 
   header_id = product_id

预期输出:

  header_id  total_amt  total_amt
  45         100        200
  22         250        500

我想找出不匹配的 total_amt。 并想展示 我想找到不匹配的 total_amt。 并想显示

我想找出不匹配的 total_amt。 并想显示

【问题讨论】:

  • 我非常怀疑一个表中的header_id 会匹配另一个表中名为product_id 的东西。您确定您的问题正确反映了数据模型吗?另外,您是否关心其中一张桌子是否缺少某些东西?

标签: sql postgresql join inner-join


【解决方案1】:

您只需要使用别名来识别不同表中具有相同标题的列:

select  
    sale_order.header_id,
    sale_order.total_amt sale_amt, 
    bill_order.total_amt bill_amt
from sale_order
join bill_order ON 
    sale_order.total_amt != bill_order.total_amt and 
    header_id = product_id
   ;

结果:

+===========+==========+==========+
| header_id | sale_amt | bill_amt |
+===========+==========+==========+
| 45        | 100      | 200      |
+-----------+----------+----------+
| 22        | 250      | 500      |
+-----------+----------+----------+

share SQL query

【讨论】:

    【解决方案2】:
    select a.header_id, a.total_amt, b.total_amt 
    from sale_order a 
    inner join bill_order b on 
        a.header_id = b.product_id 
    where a.total_amt != b.total_amt;
    
    

    未经测试,但应该可以工作。

    【讨论】:

      猜你喜欢
      • 2013-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多