mysql obtiene filas hasta un atributo de columna dado

I have a table of jobs

jobid priority (abbreviated here)

New job come into this table and they get reordered based on the priority and high priority jobs get executed first. I want to know how many jobs are ahead of me given my job id.How do I write a query for this. I am using PHP, i can even do processing after the fact, but the problem is how do I get all the rows ordered by priority up to my jobid?

Data de muestra

jobid priority (assume small number is high priority here)
--------------
 6     1
 4     1
21     1
14     1
16     2
 7     2

I want to get all the jobs before jobid 16. How do i do that.

algo como

select * from jobs order by priority asc,start_time and stop when id touches 16

preguntado el 01 de febrero de 12 a las 22:02

For two job IDs with duplicate priority, how are they ordered? -

@MarcusAdams, it looks like ties are broken with start_time based on his example. (At least, the's the assumption I made in my answer) -

3 Respuestas

SELECT * FROM tbl WHERE priority < (SELECT priority FROM tbl WHERE jobid = 16)

Respondido 02 Feb 12, 02:02

The example works only if i want to know how many jobs or before priority x. But that's not what I wanted. How about if i want to find how many jobs are before jobid 14 - Kiran

= 16 is of course just a placeholder, if you need jobid = 14 just change that oO - lugar de trabajo dinámico

@kiran, isn't it obvious that his 16 is just an example, just like your 16 was in your question? - Ben Lee

Its obvious. Now use the same query for job id 14. SELECT * FROM tbl WHERE priority < (SELECT priority FROM tbl WHERE jobid = 14). The result of sub-query is 1 and I get all the jobs of priority < 1 which does not give me any jobs. Hope you understood that - Kiran

Than just change < con <= - lugar de trabajo dinámico

In case you like JOINs (which sometimes optimize better) over subqueries:

SELECT jobs.*
FROM jobs
JOIN jobs j2
ON j2.jobid = 16
AND ((jobs.priority < j2.priority) OR
  (jobs.priority = j2.priority AND jobs.start_time < j2.start_time))
ORDER BY jobs.priority, jobs.start_time

Replace 16 with whatever job ID you're trying to return the results for.

Respondido 02 Feb 12, 17:02

Marcus, the start time of a previous job can be higher than the lower job. It went up higher in the table because the priority was increased so your solution did not work here - Kiran

+1, I think this is the best solution (yes, including compared to mine) - Ben Lee

@kiran, sorry, I forgot the order clause. I've updated the query. It should no order properly. - Marcus Adams

Estás ordenando por priority y start_time, so you need to find out what the priority and start_time of the particular record is first.

SELECT priority, start_time FROM jobs
WHERE jobid = 16 
LIMIT 1

Save these results. You'll use them in the query that actually selects the rows you want:

SELECT * FROM jobs
WHERE priority < {PRIORITY_16}
  OR (priority = {PRIORITY_16} AND start_time < {START_TIME_16})
ORDER BY priority, start_time

In the above query, replace {PRIORITY_16} y {START_TIME_16} with the values you retrieved from the first query.

Instead of using two queries like above, you can use one query with three subqueries, but that will result in four actual queries. This is how you'd do that:

SELECT * 
FROM jobs 
WHERE priority < (SELECT priority FROM tbl WHERE jobid = 16)
  OR (priority = (SELECT priority FROM tbl WHERE jobid = 16)
     AND start_time < (SELECT start_time FROM tbl WHERE jobid = 16))
ORDER BY priority, start_time

Respondido el 10 de junio de 16 a las 19:06

This does not work Ben. The start time of a job that's higher up in the table might be greater than than the one that's lower. These jobs are not permanent in order. They can be changed in priority and then that the job moves higher or lower. So for my query the only thing that i know is my job id. I want what are all the jobs before me when ordered by priority. the priorities are not unique to a job.There can be 10000 jobs with only 4 priorities 1,2,3,4 in the system. - Kiran

@kiran, check what it's doing again, your objections aren't correct. This will return todo of a higher priority, regardless of it's start_time. It will also return everything of equivalente priority that has a newer start time. This combination is precisamente the set of all rows above the one you are looking for. - Ben Lee

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.