Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.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/71.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/8/redis/2.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查询为用户和相关表提取不同的值_Mysql_Sql - Fatal编程技术网

mysql查询为用户和相关表提取不同的值

mysql查询为用户和相关表提取不同的值,mysql,sql,Mysql,Sql,我有一张名为games的桌子。 此表中有几个字段。相关字段为 用户1 用户2 第1轮 第二轮 第三轮 字段user1和user2是整数/id,当然是从表user中获取的,id和name作为字段 田野 第1轮 第二轮 第三轮 是从表格问题中获取的整数/ID 现在我需要的是创建一个sql查询,该查询将从表问号中获取ID,但只获取用户尚未使用的问号 我的想法是,当玩游戏的用户的ID等于1和2时: select from questions where id NOT IN (select round1

我有一张名为games的桌子。 此表中有几个字段。相关字段为 用户1 用户2 第1轮 第二轮 第三轮

字段user1和user2是整数/id,当然是从表user中获取的,id和name作为字段

田野 第1轮 第二轮 第三轮 是从表格问题中获取的整数/ID

现在我需要的是创建一个sql查询,该查询将从表问号中获取ID,但只获取用户尚未使用的问号

我的想法是,当玩游戏的用户的ID等于1和2时:

select from questions where id NOT IN (select round1 from questions where (user1 = 1 OR user1 = 2 OR user2 = 1 OR user2 = 2)  )
我不确定这个查询是否看起来很好,是否经过优化。然而,它还没有完成。实际上,我需要在上面的标准中添加两个,第一轮被第二轮和第三轮取代。所以最后它可能看起来像:

select from questions 
where id NOT IN (select round1 from questions where 
         (user1 = 1     OR user1 = 2 OR user2 = 1 OR user2 = 2)  )
and id NOT IN (select round2 from questions where 
         (user1 = 1 OR user1 = 2 OR user2 = 1     OR user2 = 2)  )
and id NOT IN (select round3 from questions where 
         (user1 = 1 OR user1 = 2 OR user2 = 1     OR user2 = 2)  )
另外,我可能需要在round1/round2/round3之前添加distinct


那么,你认为有更好的方法吗?提前感谢。

您可以使用UNION尝试下面的方法

这将返回游戏中第1轮、第2轮或第3轮字段中未列出的所有问题。逻辑是使用查询来建立问题ID的列表,这些问题ID在games表中用作子查询,我使用联合,因为您有3个字段,然后将其包含在where not in子句中

MySQL处理像这样的where in或not in子句的能力非常差,我不建议将此逻辑用作一个正在进行的脚本……对于数据量较低的一次尝试,它是有效的

添加:在每个联合体的子选择中包括where子句,以将其缩小到userID=1或2

答案2:where-not-in和where-in可以重写为连接…通常比where-not-in语句更快,在MySQL中更是如此,这里的引擎不能很好地处理where语句中的子选择。当在相应的表中找不到任何内容时,左联接将生成null

select q.*
from questions q
left join games g1 on g1.round1 = q.id
left join games g2 on g2.round2 = q.id
left join games g3 on g3.round3 = q.id
where g1.round1 is null and g2.round2 is null and g3.round3 is null
希望我没有弄错…您正在将问题加入游戏表,如果找不到,左加入将产生空值


尝试一下,比较一下执行计划,看看两者的性能如何。

好奇的是……这是作为一个持续的脚本运行,还是只运行一次以查找未使用的问题?在/不在选择中的位置。。。在MySQL中执行得非常糟糕,并且还有其他方式可以使用空/非空连接。实际上,每次用户与另一个用户进行匹配时,都会运行此查询。因此,理论上,这可能经常发生,这取决于比赛的数量。这不好吗?如果数据集足够大,您将遇到性能问题。我将在我的答案中发布两个答案…这将由您决定哪一个有更好的查询计划,并为您执行得更快。好的,非常感谢。我将比较性能。首先看规范化。然后尝试规范化您的设计。然后再联系我们,我们会尽可能提供帮助。
  select * from questions 
  where ID not in (select round1 from games 
                    union all 
                   select round2 from games 
                    union all 
                   select round 3 from games)
select q.*
from questions q
left join games g1 on g1.round1 = q.id
left join games g2 on g2.round2 = q.id
left join games g3 on g3.round3 = q.id
where g1.round1 is null and g2.round2 is null and g3.round3 is null