php mysql 查询类似(包含)
我正在尝试按 db 列中包含的单词过滤 sql 查询。
这是有效的(请不要告诉我“Having”是错误的......它不适用于“Where”)
$query = sprintf("SELECT *, ( 3959 * acos( cos( radians('%s') ) * cos( radians( `Lat` ) ) * cos( radians( `Long` ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( `Lat` ) ) ) ) AS distance FROM Tutors HAVING distance < '%s' ORDER BY distance",
mysql_real_escape_string($lat),
mysql_real_escape_string($lng),
mysql_real_escape_string($lat),
mysql_real_escape_string($radius));
$result = mysql_query($query, $dbConn);
我想添加以下内容:
$query = sprintf("SELECT *, ( 3959 * acos( cos( radians('%s') ) * cos( radians( `Lat` ) ) * cos( radians( `Long` ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( `Lat` ) ) ) ) AS distance FROM Tutors HAVING distance < '%s' AND SubjectList like '%s' ORDER BY distance",
mysql_real_escape_string($lat),
mysql_real_escape_string($lng),
mysql_real_escape_string($lat),
mysql_real_escape_string($radius),
mysql_real_escape_string($subject));
$result = mysql_query($query, $dbConn);
I'm trying to filter sql query by a word that is contained within a db column.
This is working (Please don't tell me the Having is wrong... it doesn't work with Where)
$query = sprintf("SELECT *, ( 3959 * acos( cos( radians('%s') ) * cos( radians( `Lat` ) ) * cos( radians( `Long` ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( `Lat` ) ) ) ) AS distance FROM Tutors HAVING distance < '%s' ORDER BY distance",
mysql_real_escape_string($lat),
mysql_real_escape_string($lng),
mysql_real_escape_string($lat),
mysql_real_escape_string($radius));
$result = mysql_query($query, $dbConn);
I'd like to add something like:
$query = sprintf("SELECT *, ( 3959 * acos( cos( radians('%s') ) * cos( radians( `Lat` ) ) * cos( radians( `Long` ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( `Lat` ) ) ) ) AS distance FROM Tutors HAVING distance < '%s' AND SubjectList like '%s' ORDER BY distance",
mysql_real_escape_string($lat),
mysql_real_escape_string($lng),
mysql_real_escape_string($lat),
mysql_real_escape_string($radius),
mysql_real_escape_string($subject));
$result = mysql_query($query, $dbConn);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是现在有效的解决方案 - 感谢大家的帮助
Here's the solution that now works - thanks for everyone's help
您是否尝试过:
WHERE 作用于所有行,而 HAVING 作用于聚合函数,因此您需要确保首先减少了聚合公式所作用的集合。这也将产生更有效的查询。
HAVING 与 WHERE 有时是一个非常棘手的区别,此链接很好地描述了它:
http://blog.sqlauthority.com/2007/07/04/sql-server-definition-comparison-and-difference- Between-having- and-where-子句/
Have you tried:
WHERE acts on all rows, whereas HAVING acts on aggregate functions, so you want to make sure that you have first reduced the set the aggregate formula is acting on. This will also yield a more efficient query.
HAVING vs. WHERE is sometimes a very sticky differentiation, this link describes it very well:
http://blog.sqlauthority.com/2007/07/04/sql-server-definition-comparison-and-difference-between-having-and-where-clause/