I am trying to get info from a table in this form :
table_1
jobid(PK) projectid desc
1 1 whatever
2 1 .
3 1 .
4 2 .
5 2 .
. . .
. . .
What I am trying to get is a query which will give me only 5 rows per projectid. ( a LIMIT on the WHERE statement but not on the whole SELECT)
If I use LIMIT, I get a total of 5 results.
解决方案
What you can do is build the SQL in a dynamic form using the following query:
SELECT GROUP_CONCAT(
DISTINCT CONCAT(
'(select jobid, projectid, desc from jobs where projectid=',
projectid,
' order by jobid limit 5)')
SEPARATOR ' union ') AS q
FROM table_1;
Save the result into a variable, and then execute the saved SQL.