MySQL 在第一个条目不匹配后取消请求
MySQL is cancelling request after first entry is mismatch
所以我写了一个小程序,它应该通过 XAMPP 从我在我的计算机上托管的数据库中获取某些信息。
程序里面的请求是这样的:
SELECT priority, platform FROM jobs WHERE priority = (SELECT MAX(priority) WHERE `platform` != '<the platform needed>')
例如,如果我给它“linux”作为平台,它应该给我结果,其中平台不是 linux 并且优先级最高。只要 'platform' 不是 table 的第一个条目中的 linux,它本身就可以正常工作。但是,如果它是 linux,数据库只是 returns 什么都没有(没有错误,它只是看到作业已完成)
例如他有这个 Table:
linux 0
windows 0
windows 1
如果我给它指令
SELECT priority, platform
FROM jobs
WHERE priority = (SELECT MAX(priority)
WHERE `platform` != 'windows')
它 returns (linux, 0) 因为这是唯一符合所有条件的条目。
但是如果我给它指令
SELECT priority, platform
FROM jobs
WHERE priority = (SELECT MAX(priority)
WHERE `platform` != 'linux')
它 returns 没什么,因为在这种情况下第一行会不匹配。 (通过调整条目的顺序和优先级证实了这一理论)
如果我直接通过 SQL 面板给出这些指令也是一样的,所以我认为这与程序本身没有任何关系。
这是处理请求的程序中的代码:
db = con.con(host, user, password, database, port)
cursor = db.cursor()
sql = 'SELECT priority, platform FROM jobs WHERE priority = (SELECT MAX(priority) WHERE `platform` ! '<the platform needed>')'
try:
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
priority = row[0]
platform = row[1]
# redirect to another script
except:
return False
有人知道为什么会这样吗?
对于 mariadb 在子查询中使用,在主查询中使用测试平台
DROP TABLE IF EXISTS T;
create table t
(platform varchar(20), priority int);
insert into t values
('linux' ,0),
('freebsd' ,0),
('freedos' ,1),
('windows', 0),
('windows' ,1);
SELECT priority, platform
FROM t
WHERE priority = (SELECT MAX(priority) from t
WHERE `platform` != 'windows')
and platform <> 'windows'
;
+----------+----------+
| priority | platform |
+----------+----------+
| 1 | freedos |
+----------+----------+
1 row in set (0.001 sec)
SELECT priority, platform
FROM t
WHERE priority = (SELECT MAX(priority) from t
WHERE `platform` != 'linux')
and platform <> 'linux'
;
+----------+----------+
| priority | platform |
+----------+----------+
| 1 | freedos |
| 1 | windows |
+----------+----------+
所以我写了一个小程序,它应该通过 XAMPP 从我在我的计算机上托管的数据库中获取某些信息。
程序里面的请求是这样的:
SELECT priority, platform FROM jobs WHERE priority = (SELECT MAX(priority) WHERE `platform` != '<the platform needed>')
例如,如果我给它“linux”作为平台,它应该给我结果,其中平台不是 linux 并且优先级最高。只要 'platform' 不是 table 的第一个条目中的 linux,它本身就可以正常工作。但是,如果它是 linux,数据库只是 returns 什么都没有(没有错误,它只是看到作业已完成)
例如他有这个 Table:
linux 0
windows 0
windows 1
如果我给它指令
SELECT priority, platform
FROM jobs
WHERE priority = (SELECT MAX(priority)
WHERE `platform` != 'windows')
它 returns (linux, 0) 因为这是唯一符合所有条件的条目。
但是如果我给它指令
SELECT priority, platform
FROM jobs
WHERE priority = (SELECT MAX(priority)
WHERE `platform` != 'linux')
它 returns 没什么,因为在这种情况下第一行会不匹配。 (通过调整条目的顺序和优先级证实了这一理论)
如果我直接通过 SQL 面板给出这些指令也是一样的,所以我认为这与程序本身没有任何关系。
这是处理请求的程序中的代码:
db = con.con(host, user, password, database, port)
cursor = db.cursor()
sql = 'SELECT priority, platform FROM jobs WHERE priority = (SELECT MAX(priority) WHERE `platform` ! '<the platform needed>')'
try:
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
priority = row[0]
platform = row[1]
# redirect to another script
except:
return False
有人知道为什么会这样吗?
对于 mariadb 在子查询中使用,在主查询中使用测试平台
DROP TABLE IF EXISTS T;
create table t
(platform varchar(20), priority int);
insert into t values
('linux' ,0),
('freebsd' ,0),
('freedos' ,1),
('windows', 0),
('windows' ,1);
SELECT priority, platform
FROM t
WHERE priority = (SELECT MAX(priority) from t
WHERE `platform` != 'windows')
and platform <> 'windows'
;
+----------+----------+
| priority | platform |
+----------+----------+
| 1 | freedos |
+----------+----------+
1 row in set (0.001 sec)
SELECT priority, platform
FROM t
WHERE priority = (SELECT MAX(priority) from t
WHERE `platform` != 'linux')
and platform <> 'linux'
;
+----------+----------+
| priority | platform |
+----------+----------+
| 1 | freedos |
| 1 | windows |
+----------+----------+