Best way to get most relevant data from table MYSQL
I have an entry
table with the columns as shown below:
id | ans_1 | ans_2 | ans_3 | ans_4 | ans_5 | date
Some sample data in the table as shown below:
1 | foo | bar | foobar | bar foo | foofoo | 2011/9/15**
2 | foo2 | bar2 | foobar2 | bar2 foo | foofoo2 | 2011/9/17**
3 | foo3 | bar3 | foobar3 | bar foo3 | foo3foo | 2011/9/20**
I would like to search for the most relevant data from the entry
table above provided by 5 answer sets.
My solution #1:
SELECT * FROM entry
WHERE ans_1 LIKE '%$answer_set_1_en%'
AND ans_2 LIKE '%answer_set_2_en%'
AND ans_3 LIKE'%answer_set_3_en%'
AND ans_4 LIKE '%answer_set_4_en%'
AND ans_5 LIKE '%answer_set_5_en%';
However, I have 2 sets of answers (diff language sets) to compare now, here comes my solution for #2:
SELECT * FROM entry
WHERE (ans_1 LIKE '%$answer_set_1_en%'
AND ans_2 LIKE '%answer_set_2_en%'
AND ans_3 LIKE '%answer_set_3_en%'
AND ans_4 LIKE '%answer_set_4_en%'
AND ans_5 LIKE '%answer_set_5_en%')
OR
(ans_1 LIKE '%$answer_set_1_jp%'
AND ans_2 LIKE '%answer_set_2_jp%'
AND ans_3 LIKE '%answer_set_3_jp%'
AND ans_4 LIKE '%answer_set_4_jp%'
AND ans_5 LIKE '%answer_set_5_jp%');
But, entry may mix with two languages, so my third attempts:
SELECT * FROM entry
WHERE (
(ans_1 LIKE '%$answer_set_1_en%' OR ans_1 LIKE '%$answer_set_1_jp%')
AND (ans_2 LIKE '%answer_set_2_en%' OR ans_2 LIKE '%$answer_set_2_jp%')
AND (ans_3 LIKE '%answer_set_3_en%' OR ans_3 LIKE '%$answer_set_3_jp%')
AND (ans_4 LIKE '%answer_set_4_en%' OR ans_4 LIKE '%$answer_set_4_jp%')
AND (ans_5 LIKE '%answer_set_5_en%' OR ans_5 LIKE '%$answer_set_开发者_StackOverflow中文版5_jp%'));
Is the third query optimized/efficient enough already? May I know any other alternative ways of querying the most relevant columns?
If you want to allow for a mix of english and japanes answeres then this looks like the way to go. Note that if you get a lot of entries this aproach will kill you, doing that many LIKE compares is going to be really slow.
Could you give examples on a valid answer_set?
Another approach is to do this same compare at the time of saving the entry (application side or with a trigger) that besides storing the actual answers also stores how many of them are correct. Then a simple
select * from entry where correct_answers = 5
is going to give you the wanted rows really quick.
精彩评论