这是我的疑问:
$sql = "SELECT * , (3956 * 2 * ASIN(SQRT( POWER(SIN(('$lat' - lat) * pi()/180 / 2), 2) +COS('$lat' * pi()/180) * COS(lat * pi()/180) * POWER(SIN(('$lon' - lon) * pi()/180 / 2), 2) ))) as distance from posts WHERE posts.subname LIKE '%$subject%' AND posts.pricing <= '$rate' ".$add_here." GROUP BY posts.UUID having distance <= '$distance' order by distance";
在浏览器中
SELECT * ,
(3956 * 2 * ASIN(SQRT( POWER(SIN(('2.993518' - lat) * pi()/180 / 2), 2) +
COS('2.993518' * pi()/180) * COS(lat * pi()/180)
* POWER(SIN(('101.7874058' - lon) * pi()/180 / 2), 2) )))
as distance
from posts
WHERE posts.subname LIKE '%tamil%'
AND posts.pricing <= '161'
AND posts.morning IN ('sun')
OR posts.afternoon IN ('sun') OR posts.evening IN ('sun','mon')
GROUP BY posts.UUID
having distance <= '40'
order by distance
我遇到的问题是,此查询的结果不会过滤子名称。它只是过滤AND posts.pricing <= '161' AND posts.morning IN ('sun') OR posts.afternoon IN ('sun') OR posts.evening IN ('sun','mon')
。
只要早晨,下午和晚上的字段匹配结果,而不会过滤subname LIKE %tamil%
。
我希望查询在上午,下午和晚上字段中搜索与subname LIKE %tamil%.
相匹配的行。请如何改变这种方式?
答案 0 :(得分:2)
问题出在你的WHERE子句中。
WHERE posts.subname LIKE '%tamil%'
AND posts.pricing <= '161'
AND posts.morning IN ('sun')
OR posts.afternoon IN ('sun') OR posts.evening IN ('sun','mon')
如果我理解你,你试图说选择: '%tamil%'和定价&lt; 161 AND(上午或下午或晚上)
因为您已经省略了括号,所以该条款在下午或晚上都是正确的。
我相信你想这样做:
WHERE posts.subname LIKE '%tamil%'
AND posts.pricing <= '161'
AND (posts.morning IN ('sun')
OR posts.afternoon IN ('sun')
OR posts.evening IN ('sun','mon'))