php - making 2 columns unique despite of order MySQL -


i have 2 fields in database set constraint of unique key. need have 2 fields unique i'm using determine whether duplicate inserted , instead update fields. query insert/update them:

insert quiz(player1, player2, array, email1, email2)  values (:user1, :user2, :quests, :email1, :email2)  on duplicate key update player1=:user1, player2=:user2, array=:quests, email1=:email1, email2=:email2" 

the unique fields player1 , player2.

that works fine count order in values inserted player1 , player2 fields. therefore want values inserted player1, player2 -> e.g. user1, user2 treated same user2, user1. possible set in database - or need additional functions in php update?

i've come across situation many times , short answer think you'll have add php code check if user2, user1 record exists before inserting user1, user2 record.


alternatively, might consider altering db this:

matches table  id  created_at  updated_at  participants table  id  user_id  match_id  created_at  updated_at  users table  id  username  email  etc...  quiz table  id  match_id  etc... 

this might seem more work @ first, has proven lot easier work in long run because allow query data lot easier if plan on doing more advanced queries. unique index on user_id , match_id. if need done quick , dirty, go way have , add php validation. if want done correctly in long run , plan on doing more advanced queries, should consider changing table structure.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -