在MySql中获取其销售价格已针对特定位置更改的那些行

时间:2019-06-18 09:28:17

标签: mysql sql

我有一个包含1000行的表,在这些行中,我想根据某些参数进行过滤。 下面提到的是我的查询

SELECT product, selling_price, location FROM tablename ORDER BY product, location

上面的查询返回下面的数据

+--------------+----------------+----------------------+
| product      | selling_price  | location             |
+--------------+----------------+----------------------+
| Product A    | 1.00           | location 1           |
+--------------+----------------+----------------------+
| Product A    | 1.00           | location 1           |
+--------------+----------------+----------------------+
| Product B    | 1.00           | location 1           |
+--------------+----------------+----------------------+
| Product B    | 2.00           | location 1           |
+--------------+----------------+----------------------+
| Product C    | 1.00           | location 1           |
+--------------+----------------+----------------------+
| Product C    | 1.00           | location 1           |
+--------------+----------------+----------------------+
| Product C    | 2.00           | location 2           |
+--------------+----------------+----------------------+
| Product C    | 2.00           | location 2           |
+--------------+----------------+----------------------+
| Product D    | 3.00           | location 1           |
+--------------+----------------+----------------------+
| Product D    | 4.00           | location 1           |
+--------------+----------------+----------------------+
| Product D    | 4.00           | location 2           |
+--------------+----------------+----------------------+
| Product D    | 4.00           | location 2           |
+--------------+----------------+----------------------+

我想编辑上面的查询,以便获得下面提到的输出。

+--------------+----------------+----------------------+
| product      | selling_price  | location             |
+--------------+----------------+----------------------+
| Product B    | 1.00           | location 1           |
+--------------+----------------+----------------------+
| Product B    | 2.00           | location 1           |
+--------------+----------------+----------------------+
| Product D    | 3.00           | location 1           |
+--------------+----------------+----------------------+
| Product D    | 4.00           | location 1           |
+--------------+----------------+----------------------+

在这种情况下,只有在相同位置更改了价格的那些行才应出现,在这种情况下,产品B和产品D的价格在特定位置发生了变化。

我尝试添加HAVING COUNT(product) > 1,但是它仅返回一行。

Display only those rows, whose SUM(price) is more than a certain number建议添加HAVING子句,但它仅适用于GROUP BY

其他链接MYSQL query - get rows where price is changed与我想要的链接非常接近,答复不是很清楚。

谢谢。

4 个答案:

答案 0 :(得分:4)

自我加入将实现:

SELECT DISTINCT
  t1.product, t1.selling_price, t1.location 
FROM tablename t1 INNER JOIN tablename t2
ON t1.product = t2.product AND t1.location = t2.location AND t1.selling_price <> t2.selling_price 
ORDER BY t1.product, t1.location, t1.selling_price

请参见demo
结果:

| product   | selling_price    | location   |
| --------- | ---------------- | ---------- |
| Product B | 1.00             | location 1 |
| Product B | 2.00             | location 1 |
| Product D | 3.00             | location 1 |
| Product D | 4.00             | location 1 |

答案 1 :(得分:1)

您可以使用EXISTS来检查其他任何具有相同产品和位置但价格不同的记录。使用DISTINCT消除可能的重复项。

SELECT DISTINCT
       t1.product,
       t1.selling_price,
       t1.location
       FROM tablename t1
       WHERE EXISTS (SELECT *
                            FROM tablename t2
                            WHERE t2.procuct = t1.product
                                  AND t2.location = t1.location
                                  AND t2.selling_price <> t1.selling_price);

答案 2 :(得分:1)

我很可能会去

查询

SELECT 
 *
FROM 
 tablename
WHERE
 (product, location) IN (

  SELECT 
     product
   , location
  FROM 
   tablename 
  GROUP BY 
     product
   , location
  HAVING 
    MIN(selling_price) <> MAX(selling_price)

)
ORDER BY 
   product
 , location

结果

| product   | selling_price | location   |
| --------- | ------------- | ---------- |
| Product B | 1             | location 1 |
| Product B | 2             | location 1 |
| Product D | 3             | location 1 |
| Product D | 4             | location 1 |

请参阅demo

答案 3 :(得分:0)

SELECT 
    product, selling_price, location
FROM
    tablename
WHERE
    product IN (SELECT 
            t.product
        FROM
            tablename t
        GROUP BY t.product , t.location
        HAVING COUNT(*) > 1
            AND (SELECT 
                COUNT(*)
            FROM
                tablename t2
            WHERE
                t2.product = t.product
                    AND t2.location = t.location
            GROUP BY t2.selling_price) > 1)
ORDER BY product , location