- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
以下查询在使用 MySql Workbench 运行时返回“5”,但使用 PHP 时针对相同的本地 apache 服务器返回 null:
SELECT @distance_in_km from branch where (@distance_in_km := 5) limit 1
另一方面,当查询是
SELECT 5 as distance_in_km from branch limit 1
Workbench 和 PHP 都返回 5。
我知道我可以通过如下修改第一个查询来解决它(让字段 distance_in_km 的值为 5 作为查询的结果):
SELECT 5 as distance_in_km, @distance_in_km from branch where (@distance_in_km := 5) limit 1
在这种情况下,workbench 返回“5,5”,PHP 返回“5, null”,这对我来说很好,因为我使用 json 获取值,因此我可以查找字段“distance_in_km”而不是“@distance_in_km”。
但是,我的实际查询要复杂得多,我想避免两次计算字段“distance_in_km”。
编辑 1
问题似乎出在 PHP 计算用户变量“distance_in_km”的方式上。在进一步测试中,我发现尽管 PHP 返回空值,但它确实知道它是真实值,因为在添加 WHERE 测试时,PHP 会正确计算:
SELECT @distance_in_km from branch where (@distance_in_km := 5) <= 4 limit 1
PHP 返回 0 条记录,而将上述测试中的 4 更改为 5,PHP 返回 1 条记录。
另一方面,PHP 在添加 'ORDER BY @distance_in_km' 子句时没有正确排序(它返回按主键排序的记录,这表明 PHP 使用的是 '@distance_in_km' 的空值,而不是它的真正的值(value)。
编辑 2
PHP 5.5.12 版、Apache 2.4.9 版、MYSQL 5.6.17 版(均取自 wampserver 2.5 菜单)。
从同一个菜单中,我打开了 MySQL 控制台,并运行上面的查询并从 PHP 中获得了结果。这是 session
使用控制台:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 476
Server version: 5.6.17 MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use q;
Database changed
mysql> SELECT @distance_in_km from branch where (@distance_in_km := 5) limit 1;
+-----------------+
| @distance_in_km |
+-----------------+
| NULL |
+-----------------+
1 row in set (0.00 sec)
mysql> SELECT @distance_in_km from branch where (@distance_in_km := 5) <= 4 limit 1;
Empty set (0.00 sec)
mysql> SELECT @distance_in_km from branch where (@distance_in_km := 5) <= 5 limit 1;
+-----------------+
| @distance_in_km |
+-----------------+
| 5 |
+-----------------+
1 row in set (0.00 sec)
mysql>
PHP 代码
$query = "SELECT @distance_in_km from branch where (@distance_in_km := 5) <= 5 limit 1";
$result = $db->query($query) or die('Err: Table not found: ' . mysql_error());
echo json_encode($result->fetch_row());
编辑 3
根据@Stan 建议的修改后的 SELECT 语句,我尝试更改我的代码但没有成功。可以看到 distance_in_km 没有从 branch 获得正确的值。
mysql> SELECT @distance_in_km, latitude from branch, (SELECT @distance_in_km := latitude from branch) as uservar limit 3;
+-----------------+-----------+
| @distance_in_km | latitude |
+-----------------+-----------+
| 32.0616233 | 32.180644 |
| 32.0616233 | 32.195598 |
| 32.0616233 | 32.197176 |
+-----------------+-----------+
3 rows in set (0.00 sec)
编辑 4
SELECT @distance_in_km, co.*, c.*
FROM `branch` c
LEFT JOIN open_hours co ON c.open = co.id,
(SELECT @distance_in_km := 111.1111 * DEGREES(ACOS(COS(RADIANS(latitude)) *
COS(RADIANS('32.177242')) * COS(RADIANS(longitude) - RADIANS('34.863834')) + SIN(RADIANS(latitude)) *
SIN(RADIANS('32.177242')))) from branch) as uservar
WHERE c.company='1' and @distance_in_km <= 20
ORDER BY @distance_in_km LIMIT 5 OFFSET 0;
编辑 5
在出现更好的解决方案之前,我结束了将计算定义为 UDF(用户定义的函数)并且我在同一个 SELECT 中调用它 3 次!!!。效率很低,但我别无选择。因此 SELECT 现在看起来:
SELECT distance_in_km(latitude, 32.177242, longitude, 34.863834) as km, c.latitude, c.longitude, c.id FROM `branch` c
WHERE c.company='1' and distance_in_km(latitude, 32.177242, longitude, 34.863834) <= 20
ORDER BY distance_in_km(latitude, 32.177242, longitude, 34.863834) LIMIT 5 OFFSET 0
在进一步测试中,如果我在 ORDER BY 中使用变量 km 看起来它也有效,这样我在 SELECT 中有两次函数
SELECT distance_in_km(latitude, 32.177242, longitude, 34.863834) as km, c.latitude, c.longitude, c.id FROM `branch` c
WHERE c.company='1' and distance_in_km(latitude, 32.177242, longitude, 34.863834) <= 20
ORDER BY km LIMIT 5 OFFSET 0
最佳答案
您可能应该阅读以下内容:MySQL user variables guide
具体来说:
As a general rule, other than in SET statements, you should never assign a value to a user variable and read the value within the same statement. For example, to increment a variable, this is okay:
SET @a = @a + 1; For other statements, such as SELECT, you might get the results you expect, but this is not guaranteed. In the following statement, you might think that MySQL will evaluate @a first and then do an assignment second:
SELECT @a, @a:=@a+1, ...; However, the order of evaluation for expressions involving user variables is undefined.
简短版本:在您的案例中,变量在查询执行后被绑定(bind),因此它没有任何值(value)。尝试在同一 session 中执行您的查询两次,作为此类“未定义”行为的证据。
更新:如果您真的想绑定(bind)变量并在同一语句中使用它,您可能想尝试从子查询中进行选择,如下所示:
SELECT @distance_in_km from branch, (SELECT @distance_in_km := 5) as uservar limit 1
我认为这是不好的做法,但它确实有效,而且我知道有时您别无选择,所以我想请明智地使用它。
关于php 返回 null 而 mysql 返回用户变量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32911163/
我的问题:非常具体。我正在尝试想出解析以下文本的最简单方法: ^^domain=domain_value^^version=version_value^^account_type=account_ty
好吧,这就是我的困境: 我正在为 Reddit 子版 block 开发常见问题解答机器人。我在 bool 逻辑方面遇到了麻烦,需要一双更有经验的眼睛(这是我在 Python 中的第一次冒险)。现在,该
它首先遍历所有 y 值,然后遍历所有 x 值。我需要 X 和 y 同时改变。 For x = 3 To lr + 1 For y = 2 To lr anyl.Cells(x, 1)
假设我有一个包含 2 列的 Excel 表格:单元格 A1 到 A10 中的日期和 B1 到 B10 中的值。 我想对五月日期的所有值求和。我有3种可能性: {=SUM((MONTH(A1:A10)=
如何转换 Z-score来自 Z-distribution (standard normal distribution, Gaussian distribution)到 p-value ?我还没有找到
我正在重写一些 Javascript 代码以在 Excel VBA 中工作。由于在这个网站上搜索,我已经设法翻译了几乎所有的 Javascript 代码!但是,有些代码我无法准确理解它在做什么。这是一
我遇到过包含日期格式的时间戳日期的情况。然后我想构建一个图表,显示“点击”项目的数量“每天”, //array declaration $array1 = array("Date" => 0); $a
我是scala的新手! 我的问题是,是否有包含成员的案例类 myItem:Option[String] 当我构造类时,我需要将字符串内容包装在: Option("some string") 要么 So
我正在用 PHP 创建一个登录系统。我需要用户使用他或她的用户名或电子邮件或电话号码登录然后使用密码。因为我知道在 Java 中我们会像 email==user^ username == user 这
我在 C++ 项目上使用 sqlite,但是当我在具有文本值的列上使用 WHERE 时出现问题 我创建了一个 sqlite 数据库: CREATE TABLE User( id INTEGER
当构造函数是显式时,它不用于隐式转换。在给定的代码片段中,构造函数被标记为 explicit。那为什么在 foo obj1(10.25); 情况下它可以工作,而在 foo obj2=10.25; 情况
我知道这是一个主观问题,所以如果需要关闭它,我深表歉意,但我觉得它经常出现,让我想知道是否普遍偏爱一种形式而不是另一种形式。 显然,最好的答案是“重构代码,这样你就不需要测试是否存在错误”,但有时没有
这两个 jQuery 选择器有什么区别? 以下是来自 w3schools.com 的定义: [attribute~=value] 选择器选择带有特定属性,其值包含特定字符串。 [attribute*=
为什么我们需要CSS [attribute|=value] Selector根本当 CSS3 [attribute*=value] Selector基本上完成相同的事情,浏览器兼容性几乎相似?是否存在
我正在解决 regx 问题。我已经有一个像这样的 regx [0-9]*([.][0-9]{2})。这是 amont 格式验证。现在,通过此验证,我想包括不应提供 0 金额。比如 10 是有效的,但
我正在研究计算机科学 A 考试的样题,但无法弄清楚为什么以下问题的正确答案是正确的。 考虑以下方法。 public static void mystery(List nums) { for (
好的,我正在编写一个 Perl 程序,它有一个我收集的值的哈希值(完全在一个完全独立的程序中)并提供给这个 Perl 脚本。这个散列是 (string,string) 的散列。 我想通过 3 种方式对
我有一个表数据如下,来自不同的表。仅当第三列具有值“债务”并且第一列(日期)具有最大值时,我才想从第四列中获取最大值。最终值基于 MAX(DATE) 而不是 MAX(PRICE)。所以用简单的语言来说
我有一个奇怪的情况,只有错误状态保存到数据库中。当“状态”应该为 true 时,我的查询仍然执行 false。 我有具有此功能的 Controller public function change_a
我有一个交易表(针对所需列进行了简化): id client_id value 1 1 200 2 2 150 3 1
我是一名优秀的程序员,十分优秀!