Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.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 SQL-获取不存在的行_Mysql - Fatal编程技术网

Mysql SQL-获取不存在的行

Mysql SQL-获取不存在的行,mysql,Mysql,标题可能听起来很奇怪,但我会尽可能简单地解释这个问题。让我们从例子开始。我得到了一个有两列的表-id,ip。假设我有3行id为2,3,5。现在,我需要得到任何不在id 1和5之间的行,这显然是1和4。目前,我被这个问题困扰着: SELECT * FROM `votes` WHERE ip = "1.1.1.1." AND question_id BETWEEN 1 AND 5 听起来很奇怪,但很多人就是这么做的 创建一个辅助表。用于左连接 create table amfn ( -

标题可能听起来很奇怪,但我会尽可能简单地解释这个问题。让我们从例子开始。我得到了一个有两列的表-id,ip。假设我有3行id为2,3,5。现在,我需要得到任何不在id 1和5之间的行,这显然是1和4。目前,我被这个问题困扰着:

SELECT * 
FROM `votes` 
WHERE ip = "1.1.1.1."  
AND question_id BETWEEN 1 AND 5

听起来很奇怪,但很多人就是这么做的

创建一个辅助表。用于左连接

create table amfn
(   -- All My Favorite Numbers  
    id int auto_increment primary key,
    theWhat char(1) null
)engine=MyIsam;   --  <----- somewhat important

insert amfn(theWhat) values (null),(null),(null),(null),(null),(null),(null),(null),(null),(null); -- 10
insert amfn(theWhat) select theWhat from amfn;
insert amfn(theWhat) select theWhat from amfn;
insert amfn(theWhat) select theWhat from amfn;
insert amfn(theWhat) select theWhat from amfn;
insert amfn(theWhat) select theWhat from amfn;
insert amfn(theWhat) select theWhat from amfn;
insert amfn(theWhat) select theWhat from amfn;
insert amfn(theWhat) select theWhat from amfn;
insert amfn(theWhat) select theWhat from amfn;
insert amfn(theWhat) select theWhat from amfn;
insert amfn(theWhat) select theWhat from amfn;
insert amfn(theWhat) select theWhat from amfn;
insert amfn(theWhat) select theWhat from amfn;
insert amfn(theWhat) select theWhat from amfn;
insert amfn(theWhat) select theWhat from amfn;
insert amfn(theWhat) select theWhat from amfn;
insert amfn(theWhat) select theWhat from amfn;

select count(*),min(id),max(id) from amfn;
+----------+---------+---------+
| count(*) | min(id) | max(id) |
+----------+---------+---------+
|  1310720 |       1 | 1310720 |
+----------+---------+---------+
1 row in set (0.00 sec)
查询: 编辑以显示Conrad Frix的时差。
我用上面的方法创建5242880行,23.5秒。康拉德的接近,168.5秒。我会坚持我的方法:>

一个非常不寻常的要求

但是您可以通过创建1..5的辅助序列来获得所需的,然后从该序列中选择投票表中没有匹配项的所有ID


好的,我找到解决办法了-

我为每个问题id和所有ip字段创建了一个新列,并用NULL填充,然后在SQL查询中使用以下逻辑:

SELECT question_id
FROM  `votes` 
WHERE (ip =  'NULL' OR ip = ?)
GROUP BY question_id
HAVING COUNT(*) - COUNT(DISTINCT question_id) = 0
ORDER BY RAND()
LIMIT 1

结果我得到了一个尚未使用的随机行,因为它搜索重复项,如果找到任何重复项,它们将从搜索中删除。我希望我写的这篇文章是可以理解的

因此问题id小于2或大于5的行?任何不在1和5之间的行。。。这不是问题1到5之间的问题吗?第1行和第4行在哪里?我不太清楚你想在这里做什么或者为什么。不,从我写的查询中,我得到了3个id为2的结果,另一个id为3,另一个id为5,都具有相同的ip 1.1.1。,但我在这个查询中没有得到的是1和4,它甚至不在我的数据库中,但我需要以某种方式得到这两个左查询。@DonatasPetrauskas:如果没有ID为1和4的行,那么这就解释了为什么没有得到它们。也许你可以做一些事情,比如从投票中选择问题id时问题id不在哪里?出于好奇,为什么需要选择不存在的数据?@David,mysql/sql中的常见做法是使用一个值或日期表来确定是否存在数据。例如,我对下面Alex的回答所写的评论。现实世界中的一个例子是,在20年的时间框架内,每个日期有6k行。我喜欢约会。他们希望在一个月内按日销售。如果星期天和几个星期二没有打折,现在你就有问题了。所以这不是一个XY问题,而是一个常见的请求。有数字助手表,甚至20年内的每个日期都有每日销售数字,这种情况并不少见。您可能希望在proc中完成理货表的创建。相比之下,这将非常缓慢,@ConradFrix,但我会在一秒钟内对其进行测试,并让您知道您不确定为什么会这样想,甚至不知道它为什么重要。创建理货表只应执行一次。好的,我可能会错,但这来自onsy twosy插入行的经验。编辑底部以显示时间差@ConradFrix,请参阅
select a.id,v.question_id,v.ip
from amfn a
left join votes v
on v.question_id=a.id and v.ip='1.1.1.1'
where a.id between 1 and 5 and v.question_id is null;
+----+-------------+------+
| id | question_id | ip   |
+----+-------------+------+
|  1 |        NULL | NULL |
|  4 |        NULL | NULL |
+----+-------------+------+
2 rows in set (0.00 sec)     <------------- boy that is fast
SELECT id 
FROM (
  SELECT 1 AS id UNION ALL 
  SELECT 2 UNION ALL 
  SELECT 3 UNION ALL 
  SELECT 4 UNION ALL 
  SELECT 5 UNION ALL 
) AS temp 
WHERE temp.id NOT IN (SELECT DISTINCT question_id FROM votes);
SELECT question_id
FROM  `votes` 
WHERE (ip =  'NULL' OR ip = ?)
GROUP BY question_id
HAVING COUNT(*) - COUNT(DISTINCT question_id) = 0
ORDER BY RAND()
LIMIT 1