These are for mysql used in a website I'm making using php Just to clarify, the table is very large, so I want to avoid using multiple queries. Thanks Hi, I have a table like this: ID, TITLE, TYPE TYPE column has 2 types of records namely: "sent" & "from" What I am trying to do is select latest 3 records of "each" type from this table using 1 single query. Unfortunately, all I am getting is 1 record of each type... Here's the query: "select * from table group by TYPE order by ID desc limit 6" That gives just 1 record of "sent" type and 1 record of "from" type (total 2, doesn't matter how I change the "limit" part of query) What can I do to select 3 records of "sent" type and 3 records of "from" type using minimal number of queries? Please give me the query if possible. Thanks
Try this (no guarantee): select * from table where type = 'sent' order by ID limit 3 union select * from table where type = 'from' order by ID limit 3 group by type; Code (markup):
The above won't work because it would select all 6 records of "sent" if they appear first in table... ( or all 6 records of "from" if they appear first ) That would work I think, but why is it grouping if I already have 3 records of each? Also I'm not sure, but is that query reading the table twice (start to end)? Thanks
Yolu don't need grouping if you don't care in what order the records are. Or you could order by ID, type or type, ID depending on what you want to see.