gpt4 book ai didi

sql - 查找 PC 型号对

转载 作者:行者123 更新时间:2023-12-04 23:43:40 24 4
gpt4 key购买 nike

我正在尝试解决一个 sql 练习。

这是架构

个人电脑

code     int  
model varchar(50)
speed smallint
ram smallint
hd real
cd varchar(10)
price money

问题 :

Find the pairs of PC models having similar speeds and RAM. As a result, each resulting pair is shown only once, i.e. (i, j) but not (j, i).



我写了一个查询,但它显示 (i, j) 和 (j, i)。

我的查询:
select  t1.model,t2.model,t1.speed,t1.ram from pc t1 , pc t2
where t1.speed = t2.speed and t1.ram = t2.ram and t1.model != t2.model

输出 :
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

所需输出:
model   model   speed   ram
1233 1121 750 128
1233 1232 500 64
1260 1232 500 32

那么如何在输出中避免 (j ,i) 呢?

谢谢。

最佳答案

您的输出和所需输出之间的差异正是具有 t1.model < t2.model 的行.要删除这些,只需添加另一个 AND t1.model >= t2.model .但是因为您已经需要 t1.model != t2.model ,完整的查询是

select  t1.model,t2.model,t1.speed,t1.ram 
from pc t1 , pc t2
where t1.speed = t2.speed and t1.ram = t2.ram and t1.model > t2.model

关于sql - 查找 PC 型号对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6059386/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com