Now, I want to check whether any given number (n) matches the field.
It should always be a match if:
Now, how can I do this in a SELECT query? I figure that I need REGEXP and stuff, but I just can't seem to get it to work.
select * from table
where
(x is null and y is null) or
(x <= n and y is null) or
(x is null and y >= n) or
(x <= n and y >= n);
Scott Geiger
MySQL Regular Expressions [mysql.com]
Either way, I can't change it to two fields, so I need something that works the way it is now. Thanks anyway though.
I keep running into the problem that my brain keeps swapping into int compare mode. I also assume that no changes will be made to the format and there is a method to that particular madness. ;)
Not being a regex genius in any way shape or form I keep thinking of doing the work in the script but that may not make any sense depending on the number of rows returned.
SELECT * FROM table WHERE
(
field='¦'
OR
(
INSTR(field,'¦')!=1
AND
INSTR(field,'¦')!=LENGTH(field)
AND
(
LEFT(field, INSTR(field,'¦')-1)<=50
)
AND
(
SUBSTRING(field,INSTR(field,'¦')+1)>=50
)
)
OR
(
INSTR(field,'¦')=1
AND
(
SUBSTRING(field,INSTR(field,'¦')+1)>=50
)
)
OR
(
INSTR(field,'¦')!=LENGTH(field)
AND
(
LEFT(field, INSTR(field,'¦')-1)<=50
)
)
)
SELECT * FROM table WHERE
(
field='¦'
OR
(
INSTR(field,'¦')!=1
AND
INSTR(field,'¦')!=LENGTH(field)
AND
(
LEFT(field, INSTR(field,'¦')-1)<='50'
)
AND
(
SUBSTRING(field,INSTR(field,'¦')+1)>='50'
)
)
OR
(
INSTR(field,'¦')=1
AND
INSTR(field,'¦')!=LENGTH(field)
AND
(
SUBSTRING(field,INSTR(field,'¦')+1)>='50'
)
)
OR
(
INSTR(field,'¦')!=1
AND
INSTR(field,'¦')=LENGTH(field)
AND
(
LEFT(field, INSTR(field,'¦')-1)<='50'
)
)
)
This is just off the top of my head an you may have to do a little playing. If -999 is a valid value that you may be checking for then change it to a number out of range that you will be checking for.
This is a slow operation and like other people I'd highly recommened 2 columns. Running 6 functions (IFNULL,SUBSTRING,POSITION * 2) is a big penalty.
daisho.