Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 获取有限select上不存在的行_Mysql_Sql - Fatal编程技术网

Mysql 获取有限select上不存在的行

Mysql 获取有限select上不存在的行,mysql,sql,Mysql,Sql,我试图获取行项目,其中该项目不应存在于select语句中,并且返回的行数有限。顺便说一下,请不要使用IN语句,因为mysql版本不支持IN 我需要的是得到前20行的用户,如果用户在前20行中,那应该是最终列表,否则我需要显示该用户。为了实现这一点,我计划进行两次查询,然后使用union,但不知道为什么我的查询在应该返回1条记录的地方总是返回零结果。下面是我对获取前20名中不存在的记录的查询: select t.* from user t where t.iduser=39 and not exi

我试图获取行项目,其中该项目不应存在于select语句中,并且返回的行数有限。顺便说一下,请不要使用IN语句,因为mysql版本不支持IN

我需要的是得到前20行的用户,如果用户在前20行中,那应该是最终列表,否则我需要显示该用户。为了实现这一点,我计划进行两次查询,然后使用union,但不知道为什么我的查询在应该返回1条记录的地方总是返回零结果。下面是我对获取前20名中不存在的记录的查询:

select t.*
from
user t
where t.iduser=39
and not exists (select u.* from user u order by points desc limit 20)
我希望得到id 39的整行,但返回的结果为零

我还尝试了以下查询:

select t.*
from
(select u.* from user u order by points desc limit 20) u,
user t
where t.iduser <> u.iduser
and t.iduser = 39
但仍然没有为我工作过。其中,如果我将id更改为11,其中iduser 11位于前20位,并将into更改为=它将返回11行,而不是预期的39行。我在这件事上迷路了

根据要求,好的,只是想了解更多信息,从用户u order by points desc limit 20中选择u.*返回:
iduser 1-20,点分别为0-19 desc。我有40行,其中21-40行有0分。我想这很简单

它不起作用,因为您的notexists子句等价于true。子查询始终返回前20名用户的非空结果

如果您希望不存在,请尝试以下操作:

select t.*
from
user t
where t.iduser=39
and not exists (
  select * from (select u.* from user u order by points desc limit 20) x 
  where t.iduser=x.iduser
)
一个可能更快的解决方案是:

select t.*
from
user t
LEFT OUTER JOIN
(select u.* from user u order by points desc limit 20) u
ON t.iduser = u.iduser
where u.iduser IS NULL
and t.iduser = 39

左外连接?您的t表仅限于t.iduser=39记录。如果该id不存在,结果将如何显示前20条记录?您的查询将始终显示0条记录或最多显示1条记录。。不是吗?不,这已经够好了。这是我唯一需要的部分谢谢@Xiangpeng Zhao。这取决于KaHeL的目的。如果目标是测试给定用户是否进入前20名,那么肯定没有必要使用此连接。如果目标是找到所有不在前20名的用户,即删除t.iduser=39,那么连接方法可能会更快,这取决于平台。目的是获取不在前20名的记录,然后我将合并所有前20名记录。这就是为什么在我的例子中,左外连接是更好的解决方案。再次感谢!您有权在此数据库上创建视图吗?