-2

I have columns that look like this:

value1____value2  
value3____value4  
value2____value1  
value5____value6

Is there any way to eliminate the combination on the 3rd line, since it's essentially the same as { value1, value2 }?

qxg
  • 6,955
  • 1
  • 28
  • 36
thuy
  • 13
  • 1
  • 4
  • 3
    Is this from two separate columns? Also, what dbms are you using? – Felix Pamittan Apr 25 '16 at 04:09
  • SELECT DISTINCT CONCAT(column1, ', ', column2) FROM tablename; – Haseena P A Apr 25 '16 at 04:21
  • According to [this post](http://stackoverflow.com/questions/2115497/check-constraint-in-mysql-is-not-working), you can't use [check constraints](http://www.w3schools.com/sql/sql_check.asp) in MySql, so the only alternative I can think of is to use before update and before insert [triggers](http://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html) on your table to prevent this duplication. – Zohar Peled Apr 25 '16 at 05:24

1 Answers1

0

What about this approach?

ALTER TABLE the_table ADD CONSTRAINT my_check
UNIQUE (LEAST(column1, column2), GREATEST(column1, column2))
StanislavL
  • 56,971
  • 9
  • 68
  • 98