Category: Uncategorized

  • Receiving the correct number of rows with an inner join

    To receive the correct number of rows with an inner join we can use LIMIT and OFFSET on a sub-query.

    SELECT id,field1,field2,field3,field4,field5 FROM (SELECT id,field1,field2,field3 FROM mytable ORDER BY id LIMIT 0,10) mytabled INNER JOIN myjointable USING (id);

  • Select the first row of each group in MYSQL

    This solution depends on having a unique column you can order by. In the example it is “id”. You must have a unique column to use for ordering or else you could get more than one result per group.

    SELECT m.* FROM mytable AS m JOIN (SELECT category, MIN(id) AS id FROM mytable GROUP BY category) AS t USING (id);