Use SELECT with field + variable in MySQL
Hello i want to select value of field id
from Mysql table where
this field
+ php varibiable is between other two values. I don’t know why doesn’t work.
$variable= 1; $sin= 1; $des= 3; $query= "SELECT id AS id_ FROM users_table WHERE ((field + $variable) BETWEEN $sin AND $des) AND (otherfield = 1)";
Should works because field == 1
AND otherfield = 1
field(1) + $variable(1) == 2 // so is between $sin and $des
field is bigint(35) and othefield is Int(1)
Please sorry my english, maybe is better to edit title
I am not an expert on MySQL, but from what I know, in the expression:
WHERE field BETWEEN a AND b
Field must be a column of the table rather than a value, because what you are comparing is the value of the column, not a number. What you should do is use the variable in your BETWEEN
comparison. Something like:
"SELECT id AS id_ FROM users_table WHERE field BETWEEN ".($sin + $variable)." AND ".($des - $variable)." AND otherfield = 1"
The end result after you PHP evaluates this expression would be something like:
SELECT id AS id_ FROM users_table WHERE field BETWEEN 5 AND 8 AND otherfield = 1
I am not sure exactly what your variables represent, but you should move your operation to the comparison part.
You are using field + $variable it means a value in MySql Field must be a column of the table rather than a value.
SELECT id AS id_ FROM users_table WHERE column BETWEEN $sin + $variable AND $des - $variable AND othercolumn = 1