Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selection without duplicates. The same data, but different order

Tags:

sql

I have some query that returns this:

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

I don't need repeating data as:

1121    1233    750 128
1233    1121    750 128

It's the same, but with different order of models.

All data stores in one table. There are a lot of PC's which have various models. I need PC's which have the same speed and ram, but I have some problem (described above). Here's my query:

SELECT a.model, b.model, a.speed, a.ram
FROM PC AS a, PC AS b
WHERE a.speed = b.speed AND a.ram = b.ram AND a.model != b.model
ORDER BY a.model DESC, b.model ASC
like image 669
Eugene Starosvetskiy Avatar asked Jun 09 '26 12:06

Eugene Starosvetskiy


1 Answers

In your existing query by changing a.model != b.model to a.model > b.model will give you the desired output.

SELECT a.model, b.model, a.speed, a.ram FROM PC AS a, PC AS b
WHERE a.speed = b.speed AND a.ram = b.ram 
and a.model > b.model
ORDER BY a.model DESC, b.model ASC

It is recommended to use explicit JOIN for your queries.

You can change your query like following.

SELECT a.model, b.model, a.speed, a.ram 
FROM PC AS a
inner join  PC AS b on a.speed = b.speed and a.ram = b.ram and  a.model > b.model      
ORDER BY a.model DESC, b.model ASC
like image 159
PSK Avatar answered Jun 12 '26 11:06

PSK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!