I am trying to get the minimum value for each string element but I am always getting the minimum of all the values that I get after querying the sql.
SELECT `assigned_to`,`appointment_lat`,`appointment_long`,`appointment_time`
FROM `tb_schedules`
WHERE `assigned_to` IN (26,27)
AND ( appointment_time>'2014-09-23 07:33:00' )
GROUP BY `assigned_to`
ORDER BY (DATEDIFF('2014-09-23 07:33:00',`appointment_time`))
LIMIT 1
If I understand your question correctly, you want the next appointment to a for each id listed:
SELECT * FROM (
SELECT `assigned_to`,`appointment_lat`,`appointment_long`,`appointment_time`
FROM `tb_schedules`
WHERE `assigned_to` IN (26,27)
AND appointment_time> now()
ORDER BY 4) x
GROUP BY `assigned_to`
This query makes use of mysql's non-standard group by capability, where if you don't specify all non-aggregate columns, it will pick one row for each unique grouped value(s) - according to the docs it's a random row, but in reality it is reliably the first row, so ordering before grouping gives you a predicable row.
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加