When i perform this query:
select id as current from events where date = '{$result['date']}'
union all
(select id as previous from events where date < '{$result['date']}' order by date desc limit 1)
union all
(select id as next from events where date > '{$result['date']}' order by date asc limit 1)
it selects the ID where the date is $result['date], the date below that, and the date above that.
So in my php code i need an array like so:
Array
(
[0] => Array
(
[current] => 6
)
[1] => Array
(
[previous] => 5
)
[2] => Array
(
[next] => 7
)
)
however, the array comes out like:
Array
(
[0] => Array
(
[current] => 6
)
[1] => Array
(
[current] => 5
)
[2] => Array
(
[current] => 7
)
)
I need the assoc array to depict the right keys!
Even though in my SQL i do
select id as current
select id as previous
select id as next
they all come out as current???
any ideas?
The reason why the column is the same name is because when you
UNIONtwo or more queries, the name of the column(s) in the topmost query are always the names displayed in the final result, regardless of the column names in subsequent queries (they are essentially ignored).MySQL Union Syntax
Instead, why not get the id’s in separate columns in one query?
Which would give you something like: