gpt4 book ai didi

PHP 和 MySQL 使用相同的查询显示不同的结果

转载 作者:可可西里 更新时间:2023-11-01 06:33:35 24 4
gpt4 key购买 nike

我有一个 MySQL 查询,当直接在我的本地 MySQL 数据库上执行时它工作正常,但当通过 PHP 执行时显示不同的结果。

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] 9n401238 | 0 | 1415091268 | NULL | 7b832b56f182ad3403521589e2815f67.jpeg | 0 |
| 72 | [Import] 9n401238 | 0 | 1415091268 | NULL | f058f379ce519f1d8a2ff8c0f5003631.jpeg | 1 |
| 72 | [Import] 9n401238 | 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] 9n401238 | 0 | 1415091268 | NULL | 7b832b56f182ad3403521589e2815f67.jpeg | 0 |
| 72 | [Import] 9n401238 | 0 | 1415091268 | NULL | f058f379ce519f1d8a2ff8c0f5003631.jpeg | 0 |
| 72 | [Import] 9n401238 | 0 | 1415091268 | NULL | a4d59377bed059e3f60cccf01a69c299.jpeg | 0 |
| 72 | [Import] 9n401238 | 0 | 1415092318 | NULL | 7b832b56f182ad3403521589e2815f67.jpeg | 0 |
| 72 | [Import] 9n401238 | 0 | 1415092369 | NULL | cad008943372d984a9b74378874128f8.jpeg | 0 |
| 72 | [Import] 9n401238 | 0 | 1415092369 | NULL | 84030a64a1f546e223e6a46cbf12910f.jpeg | 0 |
| 73 | Untitled | 0 | 1415114200 | NULL | NULL | NULL |

a) 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;');

最佳答案

是的。不保证 select 子句中表达式的求值顺序。因此,变量赋值可以以不同的顺序发生,具体取决于调用查询的方式。

您可以通过将所有变量赋值放入单个表达式中来解决此问题。尝试对 t 使用此子查询:

   (SELECT id, album, thumbURL,
(@num := IF(@group = album, @num + 1,
if(@group := album, 0, 0)
)
) as count
FROM 0_lychee_photos CROSS JOIN
(SELECT @num := 0, @group := NULL) vars
WHERE album <> 0
ORDER BY album, star DESC
) t

具体解释在documentation是:

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.

关于PHP 和 MySQL 使用相同的查询显示不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26755270/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com