我需要比较两个不同表的结果以获得出现的次数

发布于 2024-12-28 11:23:02 字数 585 浏览 2 评论 0原文

我有一个包含 2 个表的数据库: 表 1 是条目(userID、number1、number2、number3、number4、number5、number6) 表 2 是结果(number1、number2、number3、number4、number5、number6)

Entries 表存储了为每个 userID 猜测的 6 个数字,并且已经有大约 1,200 行。 结果表存储 6 个数字,并且只有一行。

现在的问题是:

我需要生成一份报告 1. 将 Result 表中的 6 个数字与 Entries 表中的每一行进行比较 2. 统计用户猜对的数字。

例如, 如果结果是 1、2、3、4、5、6 并且 userA猜测了1,3,5,7,9,11,那么userA用户的正确猜测次数是3。 如果 userB 猜对了 2,8,11,12,13,24,则 userB 的正确猜测次数是 1。 如果 userC 猜测 23,24,25,26,27,28,则 userC 的正确猜测次数为 0。

因此,我的报告需要包含 1,200 行 2 列(userID 和 Correct_Number_of_Guesses)。

解决这个问题的最佳方法是什么?

I have a database with 2 tables:
Table 1 is Entries (userID, number1, number2, number3, number4, number5, number6)
Table 2 is Result (number1, number2, number3, number4, number5, number6)

The Entries table stores 6 numbers guessed for each userID and has about 1,200 rows already.
The Result table stores 6 numbers and has only one row.

The problem now is:

I need to generate a report that will
1. compare the 6 numbers in the Result table against each and every row of the Entries table
2. count the numbers a user guessed correctly.

For example,
if the Results were 1,2,3,4,5,6 and
userA guessed 1,3,5,7,9,11, then the correct number of guesses for userA user is 3.
If userB guessed 2,8,11,12,13,24, then the correct number of guesses for userB is 1.
If userC guesses 23,24,25,26,27,28, then the correct number of guesses for userC is 0.

Therefore my report needs to contain 1,200 rows with 2 columns (userID and Correct_Number_of_Guesses).

What is the best approach towards this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狼亦尘 2025-01-04 11:23:02
SELECT userID
     , Case When t1.number1 = t2.number1 Then 1 Else 0 End +
          Case When t1.number2 = t2.number2 Then 1 Else 0 End +
          Case When t1.number3 = t2.number3 Then 1 Else 0 End +
          Case When t1.number4 = t2.number4 Then 1 Else 0 End +
          Case When t1.number5 = t2.number5 Then 1 Else 0 End +
          Case When t1.number6 = t2.number6 Then 1 Else 0 End as CorrectGuesses
  FROM Table1 t1
  cross join Table2 t2
SELECT userID
     , Case When t1.number1 = t2.number1 Then 1 Else 0 End +
          Case When t1.number2 = t2.number2 Then 1 Else 0 End +
          Case When t1.number3 = t2.number3 Then 1 Else 0 End +
          Case When t1.number4 = t2.number4 Then 1 Else 0 End +
          Case When t1.number5 = t2.number5 Then 1 Else 0 End +
          Case When t1.number6 = t2.number6 Then 1 Else 0 End as CorrectGuesses
  FROM Table1 t1
  cross join Table2 t2
囚你心 2025-01-04 11:23:02

使用 MySQL IF 语法

SELECT 
     userID,
      if( t1.number1 = t2.number1, 1, 0 )+
      if( t1.number2 = t2.number2, 1, 0 )+
      if( t1.number3 = t2.number3, 1, 0 )+
      if( t1.number4 = t2.number4, 1, 0 )+
      if( t1.number5 = t2.number5, 1, 0 )+
      if( t1.number6 = t2.number6, 1, 0 ) 
     as Correct_Number_of_Guesses
  FROM 
     Table1 t1
       cross join 
     Table2 t2

With MySQL IF syntax:

SELECT 
     userID,
      if( t1.number1 = t2.number1, 1, 0 )+
      if( t1.number2 = t2.number2, 1, 0 )+
      if( t1.number3 = t2.number3, 1, 0 )+
      if( t1.number4 = t2.number4, 1, 0 )+
      if( t1.number5 = t2.number5, 1, 0 )+
      if( t1.number6 = t2.number6, 1, 0 ) 
     as Correct_Number_of_Guesses
  FROM 
     Table1 t1
       cross join 
     Table2 t2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文