The having by clause needs to be at the end
So
$sql = "SELECT … column names … (calculation for distance)… as distance
FROM table
WHERE col1 = 'value1' AND col2 IN ('value2' , 'a different value')
HAVING distance < 1"
Frage
I am having a bit of trouble with a select statement. See my code, for brevity I removed unimportant elements.
$sql = "SELECT … column names … (calculation for distance)… as distance
FROM table
WHERE col1 = 'value1' AND col2 = 'value2' HAVING distance < 1
The above code works perfectly fine. However, when I a attempt to add an OR statement because I want another column to search for multiple values there appears to be a conflict with the query. It works without HAVING clause but than I lose that search option if removed. I get an error that says it is no the right syntax when changed to code below.
$sql = "SELECT … column names … (calculation for distance)… as distance
FROM table
WHERE col1 = 'value1' AND col2 = 'value2' HAVING distance < 1
OR col1 = 'value1' AND col2 = 'a different value' HAVING distance < 1
Any thoughts on this, I am not sure what the conflict would be. Perhaps there is a better way to search with an OR statement for multiple values for one column.
Lösung
The having by clause needs to be at the end
So
$sql = "SELECT … column names … (calculation for distance)… as distance
FROM table
WHERE col1 = 'value1' AND col2 IN ('value2' , 'a different value')
HAVING distance < 1"
Andere Tipps
Hard to give a good answer without a sqlfiddle or the entire sql query, but what about :
SELECT … column names … (calculation for distance)… as distance
FROM table
WHERE (col1 = 'value1' AND col2 = 'value2' OR col1 = 'value1' AND col2 = 'a different value')
HAVING distance < 1