0

I have a query result like this img:

enter image description here

I want to limit result by column:ID values because the number of rows is unpredictable.(in this example, i want top two ID records. but you can see ID=12 has 5 rows and ID=7 has 6 rows.) I can't group by ID because each row has special values. Thank you :)

  • E.g. (and crudely): `SELECT x.* FROM my_table x JOIN (SELECT DISTINCT id FROM my_table ORDER BY id LIMIT 2) y ON y.id = x.id;` – Strawberry Jul 18 '16 at 08:52
  • @Strawberry it seems true but not efficient. because the table is result of a heavy query and it is already expensive. is n't any SQL statement? – Mohammad Javad Khademian Jul 18 '16 at 09:29
  • Yes, a solution using variables would be more efficient. But if you want a better answer (from me), then you need to ask a better question. ;-) I note that the PRIMARY KEY is formed on (id,user_id,field_name). This is perfectly reasonable - but it makes the problem a little harder to comprehend (for those of us whose Arabic is not so great). – Strawberry Jul 18 '16 at 09:45
  • @Strawberry this not Arabic! It's Persian :) in this query result, id refers to a product. each product has some fields (counts of fields is unpredictable). But i don't want all product. i just want a limit numbers with all related fields. – Mohammad Javad Khademian Jul 18 '16 at 09:58
  • See. Mine is even worse than I thought! – Strawberry Jul 18 '16 at 09:58

0 Answers0