1

在下面的示例表中,所有具有相同价格的记录O_Id也应该具有相同的价格。显然,对于O_Id1 和 3,并非所有记录都具有相同的价格。

我很难编写一个查询,该查询将返回与O_Id一个或多个其他记录相同的表中的所有记录,但在OrderPrice具有相同O_id.

样品表:

+------+------------+------------+
| Type |  MySQL     | OrderPrice |
+======+============+============+
| 1    | 2008/11/12 | 1000       |
| 1    | 2008/10/23 | 2000       |
| 2    | 2008/09/02 |  700       |
| 2    | 2008/09/03 |  700       |
| 3    | 2008/08/30 | 2000       |
| 3    | 2008/10/04 | 2100       |
| 3    | 2008/08/30 | 2000       |
| 3    | 2008/10/04 | 2000       |
+------+------------+------------+

样本结果集:

+------+------------+------------+
| Type |  MySQL     | OrderPrice |
+======+============+============+
| 1    | 2008/11/12 | 1000       |
| 1    | 2008/10/23 | 2000       |
| 3    | 2008/08/30 | 2000       |
| 3    | 2008/10/04 | 2100       |
| 3    | 2008/08/30 | 2000       |
| 3    | 2008/10/04 | 2000       |
+------+------------+------------+
4

2 回答 2

1

您可以获得O_Id与以下相关的不止一种不同价格的地方:

SELECT O_Id
FROM myTable
GROUP BY O_Id
HAVING COUNT(DISTINCT OrderPrice) > 1

并将其用作内部语句以获取这些O_Ids 的所有行:

SELECT *
FROM myTable
WHERE O_Id IN (
    SELECT O_Id
    FROM myTable
    GROUP BY O_Id
    HAVING COUNT(DISTINCT OrderPrice) > 1
)
于 2013-04-12T18:23:40.203 回答
0

这是使用的解决方案Exists

Select * From OrderTable O
Where Exists(
    Select O_Id From OrderTable
    Where O_Id = O.O_Id
    Group By O_Id
    Having Count(Distinct OrderPrice) > 1 )
于 2013-04-12T19:25:47.083 回答