There is a table in which for each product there are several values.

Example table:

id | number | price 1 | 3 | 22.3 2 | 1 | 34.3 3 | 3 | 42.4 4 | 1 | 44.3 5 | 4 | 21.3 

Now the request is:

 SELECT * FROM table WHERE number IN (3,1,4) 

You need to make a request for a sample of goods with a minimum price for each number . As I understand it: you just need to do the sorting from minimum to maximum and put a limit on each number .

We use : MYSQL, PHP + CI3.

    1 answer 1

     SELECT t1.* FROM table t1 INNER JOIN ( SELECT number, MIN(price) minPrice FROM table GROUP BY number ) t2 ON t1.number = t2.number AND t1.price = t2.minPrice 

    In the subquery, we select the minimum price for each number. Then we make the INNER JOIN this sample on our table, and thus we get information about each product, which has the lowest price in its category.

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

    • Thanks for the answer! Why choose min (price)? I only need the product with the minimum price. Those. through WHERE IN, I select several products with the same number, and I need only one product with the minimum. - ka5itoshka
    • Got it. Now I will redo the request. - Dmitriy Shevchenko
    • I would be very grateful) - ka5itoshka
    • one
      @ ka5itoshka ready - Dmitry Shevchenko
    • It seems to be understandable) Thank you!) - ka5itoshka