簡體   English   中英

當mysql返回用戶變量的值時,php返回null

[英]php returns null while mysql returns value for a user variable

以下查詢在使用MySql Workbench運行時返回“5”,但在相同的本地apache服務器上使用PHP返回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返回null值,但它確實知道它的真值,因為在添加WHERE測試時,PHP會正確評估:

SELECT @distance_in_km from branch where (@distance_in_km := 5) <= 4 limit 1

PHP返回0條記錄,而在上述測試中將4更改為5,PHP返回1條記錄。

另一方面,當添加子句'ORDER BY @distance_in_km'時,PHP沒有正確排序(它返回按主鍵排序的記錄,這表示PHP使用'@distance_in_km'的空值而不是它的實際值。

編輯2

PHP版本5.5.12,Apache版本2.4.9,MYSQL版本5.6.17(全部取自wampserver 2.5菜單)。

從同一個菜單中我打開了MySQL控制台,並運行上面的查詢並從PHP獲得結果。 這是會議

使用控制台:

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沒有從分支獲得正確的值。

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用戶變量指南

特別是這個:

作為一般規則,除了在SET語句中,您不應該為用戶變量賦值並在同一語句中讀取值。 例如,要增加變量,這沒關系:

SET @a = @a + 1; 對於其他語句,例如SELECT,您可能會得到您期望的結果,但這不能保證。 在下面的語句中,您可能會認為MySQL將首先評估@a,然后再進行一次分配:

SELECT @ a,@ a:= @ a + 1,...; 但是,涉及用戶變量的表達式的評估順序是未定義的。

簡短版本:變量在您的情況下執行查詢后綁定,因此它沒有任何值。 嘗試在同一會話中執行兩次查詢,以證明此類“未定義”行為。

UPD:如果你真的想綁定變量並在同一個語句中使用它,你可能想嘗試從子查詢中選擇,如下所示:

SELECT @distance_in_km from branch, (SELECT @distance_in_km := 5) as uservar limit 1

我認為這是不好的做法,但它有效,我明白有時你沒有選擇,所以我明智地使用它。

現在從你的編輯#4看起來,好像你在這里使用用戶變量的唯一理由是你不僅要選擇計算的值,還要在WHERE子句中使用它......?

在這種情況下,你應該應該努力和重復WHERE子句中的計算(如已經指出的那樣,因為 WHERE子句就會執行這個,你不能使用動態計算出有別名“列”值。)

SELECT
  co.*,
  c.*,
  (111.1111 * DEGREES(ACOS(COS(RADIANS(latitude)) * …) AS distance_in_km
FROM `branch` c
LEFT JOIN open_hours co ON c.open = co.id
WHERE
  c.company='1'
    AND
  (111.1111 * DEGREES(ACOS(COS(RADIANS(latitude)) * …) <= 20
ORDER BY distance_in_km
LIMIT 5 OFFSET 0;

(再次使用ORDER BY子句中的別名列distance_in_km就可以了,因為這會在以后進行評估。)

現在這聽起來可能非常低效,因為它看起來像兩次相同的昂貴計算 - 但實際上,查詢優化器應該能夠識別它,因此它實際上只計算一次。

嘗試一下,看看這是否能給你帶來理想的結果 - 以及它的表現。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM