I'm having the following problem. Imagine my mySQL table looks like this:
id range
1 210-400
2 300-310
3 100-350
I want to find
Now i tried to select the needed parts of that field with SUBSTRING and then get the MIN or MAX value like this:
SELECT
SUBSTRING_INDEX(`range`,'-',1) as `left_value`,
SUBSTRING_INDEX(`range`,'-',-1) as `right_value`,
MIN(`left_value`),
MAX(`right_value`)
FROM `table`
But i just get "#1054 - Unknown column 'left_value' in 'field list'"
So my question - is this even possible, and how?
You're missing a comma after your 3rd line (after as 'right_value').
However, this probably still won't completely solve your problem. +1 for Devart's answer. To expand, you're probably looking for something closer to this:
SELECT
MIN(SUBSTRING_INDEX(`range`, '-', 1)),
MAX(SUBSTRING_INDEX(`range`, '-', -1))
FROM `table`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With