我有一個MySQL查詢,它直接在我的本地MySQL數據庫上執行時工作正常,但通過PHP執行時顯示不同的結果。PHP和MySQL顯示不同的結果與相同的查詢
SELECT a.id, a.title, a.public, a.sysstamp, a.password, t.thumbURL, t.count
FROM 0_lychee_albums AS a
LEFT JOIN (SELECT id, album, thumbURL,
@num := IF(@group = album, @num + 1, 0) AS count,
@group := album AS dummy
from 0_lychee_photos
WHERE album != 0
ORDER BY album, star DESC) AS t ON a.id = t.album
WHERE count <= 2 OR count IS NULL;
或作爲一襯墊:
SELECT a.id, a.title, a.public, a.sysstamp, a.password, t.thumbURL, t.count FROM 0_lychee_albums AS a LEFT JOIN (SELECT id, album, thumbURL, @num := IF(@group = album, @num + 1, 0) AS count, @group := album AS dummy FROM 0_lychee_photos WHERE album != 0 ORDER BY album, star DESC) AS t ON a.id = t.album WHERE count <= 2 OR count IS NULL;
其結果是:
| id | title | public | sysstamp | password | thumbURL | count |
| 71 | [Import] 01 | 0 | 1415091268 | NULL | cad008943372d984a9b74378874128f8.jpeg | 0 |
| 72 | [Import] 9n4| 0 | 1415091268 | NULL | 7b832b56f182ad3403521589e2815f67.jpeg | 0 |
| 72 | [Import] 9n4| 0 | 1415091268 | NULL | f058f379ce519f1d8a2ff8c0f5003631.jpeg | 1 |
| 72 | [Import] 9n4| 0 | 1415091268 | NULL | a4d59377bed059e3f60cccf01a69c299.jpeg | 2 |
| 73 | Untitled | 0 | 1415114200 | NULL | NULL | NULL |
PHP的結果:
| id | title | public | sysstamp | password | thumbURL | count |
| 71 | [Import] 01 | 0 | 1415091268 | NULL | cad008943372d984a9b74378874128f8.jpeg | 0 |
| 72 | [Import] 9n4| 0 | 1415091268 | NULL | 7b832b56f182ad3403521589e2815f67.jpeg | 0 |
| 72 | [Import] 9n4| 0 | 1415091268 | NULL | f058f379ce519f1d8a2ff8c0f5003631.jpeg | 0 |
| 72 | [Import] 9n4| 0 | 1415091268 | NULL | a4d59377bed059e3f60cccf01a69c299.jpeg | 0 |
| 72 | [Import] 9n4| 0 | 1415092318 | NULL | 7b832b56f182ad3403521589e2815f67.jpeg | 0 |
| 72 | [Import] 9n4| 0 | 1415092369 | NULL | cad008943372d984a9b74378874128f8.jpeg | 0 |
| 72 | [Import] 9n4| 0 | 1415092369 | NULL | 84030a64a1f546e223e6a46cbf12910f.jpeg | 0 |
| 73 | Untitled | 0 | 1415114200 | NULL | NULL | NULL |
一個)count
不增加像它應該
b)因爲a)它顯示的行數比它應該多(應該限制爲每個ID 3)
我檢查過多次,兩個查詢都完全一樣。沒有用戶輸入或PHP中的任何差異。
我已經檢查了similar questions,但沒有幫助。以下查詢在MySQL和PHP上都顯示相同的結果:
SHOW VARIABLES LIKE 'character_set%';
SHOW VARIABLES LIKE 'collation%';
是否有人知道這個問題的差異?
編輯與進一步的信息:
$database = new mysqli($host, $user, $password, $database);
$query = "SELECT a.id, a.title, a.public, a.sysstamp, a.password, t.thumbURL, t.count FROM 0_lychee_albums AS a LEFT JOIN (SELECT id, album, thumbURL, @num := IF(@group = album, @num + 1, 0) AS count, @group := album AS dummy FROM 0_lychee_photos WHERE album != 0 ORDER BY album, star DESC) AS t ON a.id = t.album WHERE count <= 2 OR count IS NULL";
$albums = $database->query($query);
while ($album = $albums->fetch_assoc()) { print_r($album); }
我也有和沒有執行查詢之前,以下試了一下:
$database->set_charset('utf8');
$database->query('SET NAMES utf8;');
num_rows()函數告訴你什麼? – 2014-11-05 10:56:15
這是不可能的,一個班輪或格式的SQL造成如此大的差異,你應該檢查你的代碼。我覺得有些東西在那裏。只有你的查詢差異是',星形DESC',但我不認爲這將使差異 – 2014-11-05 11:10:37
確保你的PHP代碼指向相同的數據庫,你正在運行此查詢 – 2014-11-05 11:15:08