简体   繁体   English

Select 表中的最新时间戳值,该表具有列 id 的多个条目,每个唯一列 id 和来自另一个表的数据

[英]Select latest timestamped value from table, which has multiple entries for a column id, for each unique column id and data from another table

I need to draw data from two tables for a school site.我需要从学校网站的两个表中提取数据。 Users flag (tFlag) topics they have studied and are ready to answer questions on.用户标记(tFlag)他们已经研究并准备回答问题的主题。 These choices are recorded in the userTopics table.这些选择记录在 userTopics 表中。 There are 55 topics (topicID), and there are many questions available (qID) for each topic on the site.有 55 个主题 (topicID),网站上的每个主题都有很多可用的问题 (qID)。

The UserTopics table also lists a rolling rating of their performance, the topicScore, for each topic. UserTopics 表还列出了每个主题的性能滚动评分 topicScore。 There will only be one unique row for any user per topicID.每个 topicID 的任何用户都只会有一个唯一的行。

UserTopics用户主题

| ID | userID | topicID | tFlag | topicScore |
| 25 | 1      | 1       | 1     | 25         |
| 29 | 1      | 2       | 1     | 70         |
| 42 | 1      | 3       | 0     | 5          |
| 41 | 1      | 5       | 0     | 5          |
| 35 | 1      | 6       | 1     | 43         |
| 31 | 1      | 7       | 1     | 62         |
| 44 | 1      | 8       | 0     | 0          |
| 32 | 1      | 9       | 0     | 5          |
| 35 | 1      | 12      | 1     | 30         |

The Results table logs the results of each answered question, and timestamps the answer time, so the table can have many records for each question answered by the user, and looks like this (other cols removed)结果表记录每个回答问题的结果,并为回答时间加上时间戳,因此该表可以为用户回答的每个问题提供许多记录,如下所示(其他列已删除)

Results结果

| resultID | userID | topicID | qID | correct | answerTime          |
| 9        | 1      | 12      | 15  | 1       | 2020-05-28 11:29:18 |
| 10       | 1      | 12      | 26  | 1       | 2020-05-28 11:30:18 |
| 11       | 1      | 1       | 132 | 0       | 2020-06-02 17:03:42 |
| 13       | 1      | 2       | 50  | 0       | 2020-06-02 17:02:53 |
| 14       | 1      | 7       | 10  | 1       | 2020-06-02 17:05:15 |
| 76       | 1      | 6       | 9   | NULL    | 0000-00-00 00:00:00 |
| 75       | 1      | 6       | 9   | NULL    | 0000-00-00 00:00:00 |
| 65       | 1      | 9       | 12  | NULL    | 0000-00-00 00:00:00 |
| 66       | 1      | 9       | 12  | 1       | 2020-06-04 07:34:02 |

In order to have the system automatically assign a question for the student, we need values for topicScore and answerTime for a specific user for every topicID that they are currently learning.为了让系统自动为学生分配问题,我们需要特定用户当前正在学习的每个 topicID 的 topicScore 和 answerTime 值。 One row must be returned for each topicID in the UserTopics table where tFlag = 1 for the user.必须为 UserTopics 表中的每个 topicID 返回一行,其中用户的 tFlag = 1。 The most recent answerTime from the Results table for each returned topicID from userTopics should then be fetched.然后应该从结果表中获取从 userTopics 返回的每个 topicID 的最新 answerTime。 If, however, the only answerTime listed for a particular topicID is 0000-00-00 00:00:00, I need that returned.但是,如果为特定 topicID 列出的唯一 answerTime 是 0000-00-00 00:00:00,我需要返回它。 (the answerTime col uses ON UPDATE CURRENT_TIMESTAMP, so if they have loaded the question but not answered it, the value here is zero.) (answerTime col 使用 ON UPDATE CURRENT_TIMESTAMP,因此如果他们已加载问题但未回答,则此处的值为零。)

A query result for userID = 1 would hopefully be: userID = 1 的查询结果希望是:

Query result查询结果

| topicID | topicScore | answerTime          |
| 1       | 25         | 2020-06-02 17:03:42 |
| 2       | 70         | 2020-06-02 17:02:53 |
| 6       | 43         | 0000-00-00 00:00:00 |
| 7       | 62         | 2020-06-02 17:05:15 |
| 12      | 30         | 2020-06-04 07:34:02 |

I have tried the following query, but it's not getting what I want, and also don't actually understand it as my grasp of mySQL is a bit basic atm.我尝试了以下查询,但它没有得到我想要的,也没有真正理解它,因为我对 mySQL 的掌握有点基本。 (query written by another) (另一个人写的查询)

SELECT
    r.userID,
    r.topicID,
    r.answerTime,
   (SELECT t.topicScore FROM UserTopics t WHERE t.userID = r.userID AND t.topicID = r.topicID) AS topicScore
    FROM Results r
    LEFT JOIN Results r2 ON r2.topicID = r.topicID AND r.answerTime < r2.answerTime
    WHERE r2.answerTime IS NULL AND r.userID = 1 
    ORDER BY `r`.`topicID` ASC

I can see it needs to have Where t.tFlag =1 somewhere, but when I put it in the bracketed where clause, it doesn't work either so I assume the whole query needs a re-write.我可以看到它需要在某处有 Where t.tFlag =1,但是当我将它放在方括号中的 where 子句中时,它也不起作用,所以我假设整个查询需要重写。 Glad for any help.很高兴有任何帮助。

Here's the query I have now tried and tested following O Jones suggestions below....needed a couple of changes but the core was just what the doctor ordered, works like a charm, thanks.这是我现在按照以下 O Jones 建议尝试和测试的查询....需要进行一些更改,但核心正是医生所要求的,就像一个魅力一样,谢谢。 Needed only one userID per query, and only results where topicScores are not 0. Also as there is only one row per topicID in the UserTopics table, didn't need the MAX statement either.每个查询只需要一个 userID,并且只需要 topicScores 不为 0 的结果。此外,由于 UserTopics 表中每个 topicID 只有一行,因此也不需要 MAX 语句。

SELECT ut.topicId, 
       ut.topicScore,
       MAX(r.answerTime) answerTime
  FROM UserTopics ut
  LEFT JOIN Results r   ON ut.userId = r.userId
                       AND ut.topicId = r.topicId
WHERE ut.tFlag =1 AND ut.userId = 1 AND ut.topicScore >0
GROUP BY ut.topicId 
ORDER BY ut.topicId

First of all, using zero datestamps makes for a brittle design.首先,使用零日期戳会使设计变得脆弱。 Later versions of MySQL disallow zero datestamps. MySQL 的更高版本不允许零日期戳。 You'll need to give this command to make things work, that is to remove the NO_ZERO_DATE mode.您需要给出此命令才能使事情正常进行,即删除NO_ZERO_DATE模式。

SET sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';

You'd be better off using NULL rather than a zero datestamp.最好使用 NULL 而不是零日期戳。 That being said...话虽如此...

This is a job for GROUP BY , where you pull out the maximum score and answerTime for each combination of userId and topicId.这是GROUP BY的工作,您可以在其中提取 userId 和 topicId 的每个组合的最大分数和 answerTime。 ( https://www.db-fiddle.com/f/swC1jG2mMHRsAkCouJNfXq/5 ) https://www.db-fiddle.com/f/swC1jG2mMHRsAkCouJNfXq/5

SELECT ut.userId, ut.topicId, 
       MAX(ut.topicScore) topicScore, 
       MAX(r.answerTime) answerTime
  FROM UserTopics ut
  LEFT JOIN Results r   ON ut.userId = r.userId
                       AND ut.topicId = r.topicId
WHERE ut.tFlag =1
GROUP BY ut.userId, ut.topicId 
ORDER BY ut.userId, ut.topicId

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从表中选择行,其中具有相同id的另一个表中的行在另一列中具有特定值 - Select rows from a table where row in another table with same id has a particular value in another column Mysql 按 id 对列组求和,然后将每个 id 的总和与另一个表中另一列的值相除,每行的 id 匹配 - Mysql sum a column group by id, then divide that sum for each id with value from another column in another table with the matching id for each row 从表中读取一列,其id是mysql中的另一个表 - Read a column from a table which its id is another table in mysql 从SQL表中选择每个唯一ID中的3个 - Select 3 of each unique ID from SQL table 通过连接另一表的另一列按ID选择名称 - Select name by id from another column with join another table 选择名为Titel的列,其中的ID与另一个表中的ID相同 - Select column called Titel where the id is the same from another Table 从表1连接表2中选择id,table1.column1 + table2.column2作为总数。 如果table2.column2没有值怎么办? - select id, table1.column1 + table2.column2 as total from table1 join table2. what if table2.column2 has no value? SQL计算列中每个不同的值,并从ID匹配的另一个表中添加名称 - SQL count each distinct value in column and add name from another table where the ID matches 从表和唯一列y中选择ID - select id from table and distinct column y 需要通过另一个表中的ID更新列 - Need to update a column by an id from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM