I’m using php + mysql, I have such table in DB:
col1-col2-col3
A - val1 - str1
A - val2 - str2
A - val3 - str3
B - val4 - str4
B - val5 - str5
...
And also assoc array in php:
$arr = array('A'=>'value1','B'=>'value2', ...);
Then I’m passing this array in placeholders in MySql string to make complete SELECT statement.
How to choose column 3 values where value of column 1 equals to key of array AND column 2 values are less than array value respectively? Maybe using somehow GROUP with two selects and joining them together…
The result should be just list of all column 3 values that satisfied requirements.
It can be done using sth like this:
$aReturn = array();
$sql = "SELECT col3 FROM MyTab WHERE col1 = ? AND col2 < ?d";
foreach ($arr as $k=>$v) {
$aReturn = array_merge($aReturn, $this->select($sql, $k, $v));
}
Where select is method which makes select to DB and returns values of last column in list of values, then we add list to result $aReturn. But in this case it will query several times,
but is there a way how to do it in only one SELECT?
I don’t think this can be done with placeholders, because the structure of the query isn’t fixed — you need as many conditions in the WHERE clause as you have elements in the array. Here’s untested code:
UPDATED: uses prepared statement