Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If your query restricts resultst to both</p> <ul> <li>the current day and</li> <li>times which still lie in the future</li> </ul> <p>then you can get the nearest of these using</p> <pre><code>SELECT … FROM … WHERE … ORDER BY hora_servicio ASC LIMIT 1 </code></pre> <p>This simply orders the resulting rows by hour and selects the first of them. After the last meal of the day, you'll have to wait till midnight before this query will return a non-empty result. You could probably best fix that using a union:</p> <pre><code>(SELECT 0 AS tomorrow, … FROM … WHERE dia = ((DAYOFWEEK(CURDATE()) + 5) MOD 7) + 1 AND hora_servicio &gt; CURTIME() ) UNION ALL (SELECT 1 AS tomorrow, … FROM … WHERE dia = DAYOFWEEK(CURDATE()) -- this really is the next day ) ORDER BY tomorrow, hora_servicio LIMIT 1 </code></pre> <p>You might need this weekday computation for your code as well. Here is how it works:</p> <pre><code> Su Mo Tu We Th Fr Sa DAYOFWEEK(CURDATE()) 1 2 3 4 5 6 7 (DAYOFWEEK(CURDATE()) + 5) 6 7 8 9 10 11 12 ((DAYOFWEEK(CURDATE()) + 5) MOD 7) 6 0 1 2 3 4 5 ((DAYOFWEEK(CURDATE()) + 5) MOD 7) + 1 7 1 2 3 4 5 6 </code></pre> <p>Your version would compute 0 instead of 7 for sunday. The fact that you can use the MySQL <code>DAYOFWEEK</code> without any magic to refer to the <em>next</em> day might seem a little confusing, so it is certainly worth a comment.</p> <p>As an alternative to <code>dia = ((DAYOFWEEK(CURDATE()) + 5) MOD 7) + 1</code> you might write <code>dia MOD 7 = DAYOFWEEK(CURDATE()) - 1</code>, as this will use zero-based numbers for both sides. However, the result might be less efficient as the database will have to compute the value of the left side, which breaks indexing and similar optimizations. So I'd rather put all the computation into the right hand side.</p> <p>Thanks to @ypercube for getting me to think about the next day at all.</p> <p>If you want to consider more than one day into the future (I hope you won't have to do that for too many people), then you could do something like this:</p> <pre><code>SELECT dia, hora_servicio FROM detalle_regimen_paciente WHERE dia &lt;&gt; ((DAYOFWEEK(CURDATE()) + 5) MOD 7) + 1 OR hora_servicio &gt; CURTIME() ORDER BY (dia - DAYOFWEEK(CURDATE()) + 1) MOD 7 ASC, hora_servicio ASC LIMIT 1 </code></pre> <p>The idea here is that the first <code>ORDER BY</code> term counts the number of days into the future: this value will be 0 for the current day, 1 for tomorrow, 2 for the day after and so on. For meals from the current day, we only take those after the current time. For all other days, any time will be fine.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload