Hello! I have a MySQL database and in it a table with 2 fields, fields of type int. There can not be several records with the same first and second field. I have an array of numbers m. I need to get the value of the first field, whose value in the second field more closely matches the numbers in the array, something like that.

I mean, I have a table:

param1 param2 1 111 2 111 2 222 2 444 3 222 3 555 

And the array:

 111 222 333 

I need to get the number 2 from the table. I would be very grateful for the help.

    2 answers 2

     select param1, count(param1) as x from tbl where param2 in (111, 222, 333) group by param1 order by x desc limit 1 

    http://sqlfiddle.com/#!2/3cdae5/2

    • Thank you very much, I learned a lot of new things. :) - bitrixhater

    After two requests: choose the first value with only one condition, write the value, and then choose with two.