I have a table with two columns:
I need to select all cooperation pairs, it is easy but in what is the problem:
table have data like: 987
- 102
, 103 - 104
, 104 - 103
, 21 - 102
.
As the result with such data i should have 3 cooperation pairs 987 - 102
, 103-104
, 21-102
, as you see 103 - 104
and 104 - 103
records have the same logic, how can I avoid duplicating of them. Any idea?
Thanks, and best regards. Anton.
The go to solution for removing duplicate rows from your result sets is to include the distinct keyword in your select statement. It tells the query engine to remove duplicates to produce a result set in which every row is unique.
If you want the query to return only unique rows, use the keyword DISTINCT after SELECT . DISTINCT can be used to fetch unique rows from one or more columns. You need to list the columns after the DISTINCT keyword.
The Insert on Duplicate Key Update statement is the extension of the INSERT statement in MySQL. When we specify the ON DUPLICATE KEY UPDATE clause in a SQL statement and a row would cause duplicate error value in a UNIQUE or PRIMARY KEY index column, then updation of the existing row occurs.
You could use MySQL's LEAST()
and GREATEST()
functions, together with DISTINCT
:
SELECT DISTINCT LEAST(a, b), GREATEST(a, b) FROM mytable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With