Click to See Complete Forum and Search --> : WHERE clause woes


bokeh
02-22-2007, 12:13 PM
SELECT
min(`$IdColumn`) AS `first`,
max(`$IdColumn`) AS `last`
FROM `$tablename`
That query works ok as does this one:SELECT
max(`$IdColumn`) AS `prev` WHERE `$IdColumn` < '$id'
FROM `$tablename`but I am trying to make a combined query something like this:SELECT
max(`$IdColumn`) AS `prev` WHERE `$IdColumn` < '$id',
min(`$IdColumn`) AS `next` WHERE `$IdColumn` > '$id',
min(`$IdColumn`) AS `first`,
max(`$IdColumn`) AS `last`
FROM `$tablename`I'm not sure of the correct syntax to use and want to avoid doing multiple queries.

russell
02-22-2007, 12:28 PM
with the caveat that i am not 100% MySQL does Case statements the same way as MS SQL Server, should be like this (works in MSSQL)

SELECT MAX(Case WHEN idCol < 3 THEN idCol END) as prev,
MIN(Case WHEN idCol > 3 THEN idCol END) as next,
MIN(idCol) as first,
Max(idCol) as last
FROM tableName


EDIT: the 3 is hard coded from my test btw...

bokeh
02-22-2007, 02:09 PM
Thanks a lot! It works fine under MySQL.

russell
02-22-2007, 03:19 PM
beauuuuutiful :)

bokeh
02-22-2007, 04:58 PM
Just so the code is linked together here is the other half of the thread on the PHP forum (http://webdeveloper.com/forum/showthread.php?p=718325#post718325).