且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQL不包括2列的组合上的重复值

更新时间:2023-02-09 20:22:42

我认为问题陈述是一个线索,你需要一个 A.model> B.model 条件某处。加入的 ON 条件听起来像一个很好的候选人:

  SELECT A.model ,B.model,A.speed,A.ram 
FROM PC A
JOIN PC B ON(A.model> B.model) - << === Here
WHERE A.speed = B.speed
AND A.ram = B.ram

<> 是对称的; > 不是。切换到> 确保如果 {i,j} 在,则 {j ,i} 肯定会出来。


I am working on exercise 16 from sql-ex.ru. The problem asks the following:

Find the pairs of PC models having identical speeds and RAM. 
As a result, each resulting pair is shown only once, i.e. (i, j) but not (j, i). 
Result set: model with higher number, model with lower number, speed, and RAM.

The database schema is :

Product(maker, model, type)
PC(code, model, speed, ram, hd, cd, price)
Laptop(code, model, speed, ram, hd, screen, price)
Printer(code, model, color, type, price)

I wrote the following query:

SELECT A.model, B.model, A.speed, A.ram
FROM PC A
JOIN PC B ON (A.model<>B.model)
WHERE A.speed=B.speed
AND A.ram=B.ram

But this displays duplicates of i,j as j,i. Here is my output:

model   model   speed   ram
1121    1233    750 128
1232    1233    500 64
1232    1260    500 32
1233    1121    750 128
1233    1232    500 64
1260    1232    500 32

As you can see, the values of i,j are flipped and counted as distinct values. Is there an easy way to get rid of duplicates like this? I am kind of lost on that part.

I think the "model with higher number, model with lower number" in the problem statement is a clue that you need to have a A.model > B.model condition somewhere. Join's ON condition sounds like a fine candidate:

SELECT A.model, B.model, A.speed, A.ram
FROM PC A
JOIN PC B ON (A.model > B.model) -- <<<=== Here
WHERE A.speed=B.speed
AND A.ram=B.ram

The <> is symmetrical; the > is not. Switching to > ensures that if {i, j} is in, then {j, i} will be out for sure.