【问题标题】:Select 1 row from each child table, but ignore previously selected values从每个子表中选择 1 行,但忽略之前选择的值
【发布时间】:2015-07-12 08:48:27
【问题描述】:

我可以就以下问题寻求帮助吗?

我的数据库中有 3 个这样的表……

wordDates
dID        wDate
1       ‘2015-01-01’
2       ‘2015-01-02’
3       ‘2015-01-03’
4       ‘2015-01-04’
5       ‘2015-01-05’

Words
wID    dID    word
1      1      dca
2      1      mno
…
28     4      xyz
29     4      abc
30     4      abc

第三个表是从查询中创建的...

CREATE TABLE faveWords
SELECT DISTINCT word, count(word) 
FROM words
GROUP BY word
ORDER BY word;

这给了我一个类似于...的表格

word    wordCount
abc     8
dca     8
mno     7
dad     6

基本上它只是一个排名词表。我有一个数据库here.

我的问题是,我想为每个 wDate 获取最上面的单词,限制完整的结果,最后忽略以前为查询的每次迭代选择的值。

想象一下单词表中有这样的值......

dID(FK)    word
1          abc    // 'abc' = Top word in faveWords 8
1          dca    

上面,查询将选择“abc”,因为它是 faveWords 中最流行的词。

2          abc   // Need to ignore this word
2          xyz    
2          dad

在下一次迭代中,我需要忽略“abc”并从“xyz”和“dad”中找到最流行的词。在我的小提琴案例中,下一个选择将是“爸爸”。

3          abc  // ignore
3          dca 
3          xyz
3          dad  // ignore

第三次迭代中唯一可供选择的值将是“dca”和“xyz”,因为“abc”和“dad”已被选择。

直到我得到两个日期之间最流行的单词列表。但是,最后,我想获得这些选择中的前 n 个(基于 faveWords.wordCount)。因此,如果我的 LIMIT 是 3,我将有一个结束数据集 ...

'abc' // This scored 8 for wordCount in faveWords from the fiddle
'dad' // This scored 6
'dca' // This scored 8

我必须生成结果的查询是......

SELECT w.wID, w.dID, wd.wDate, w.word, fw.word
FROM words w
INNER JOIN worddates wd ON wd.dID = w.dID
INNER JOIN  (
        SELECT sw.word, sfw.wordCount
        FROM words sw
            INNER JOIN worddates swd ON swd.dID = sw.dID
            INNER JOIN faveWords sfw ON sfw.word = sw.word
        WHERE swd.wDate >= '2015-01-02'
          AND swd.wDate <= '2015-01-07'
        GROUP BY sw.word
        ORDER BY sfw.wordCount DESC
        LIMIT 1
    ) AS fw ON fw.word = w.word
ORDER BY w.wID
LIMIT 3;

但是,它甚至与我想从表中检索的内容不相近。它只为我的 LIMIT 3 的每一行返回“abc”。

我可以请你帮忙吗?如何获取每天的 faveWord,但忽略以前选择的值?

是否可以将单词选择添加到可用作“忽略”表的“tempTable”中?

谢谢。

为草莓编辑:-

查询将选择...的初始列表

|   1 | 2015-01-01 |   4 | abc  |       faveWords score 8
|   2 | 2015-01-02 |   6 | dca  |       faveWords score 8
|   3 | 2015-01-03 |  12 | bcd  |       faveWords score 3
|   4 | 2015-01-04 |  15 | dad  |       faveWords score 6
|   5 | 2015-01-05 |  17 | mno  |       faveWords score 7
|   6 | 2015-01-06 |  23 | dbd  |       faveWords score 1
....

然后最终的 'LIMIT 3' 会选择

|   1 | 2015-01-01 |   4 | abc  |       faveWords score 8
|   2 | 2015-01-02 |   6 | dca  |       faveWords score 8
|   5 | 2015-01-05 |  17 | mno  |       faveWords score 7

来自初始列表。

至于为什么。我正在尝试生成一个 unique 单词的前 n 个列表,例如 3 个月,但我不想多次列出单词。我的意思是,我不想要最终的结果……

|   1 | 2015-01-01 |   4 | abc  |
|   2 | 2015-01-02 |   5 | abc  |
...
|   n | 2015-mm-dd |   x | abc  |
| n+1 | 2015-mm-dd | x+1 | abc  |

NB - 抱歉,当你到达线路时......

|   1 | 2015-01-01 |  26 | dca  |

小提琴数据存在一些问题。 dID 应该继续 7 等等......并且日期也应该增加。代表我的懒惰复制和粘贴。

编辑 2:-

Better fiddle Data

【问题讨论】:

  • 我想我已经找到了解决这个问题的方法。将测试和更新 Q。

标签: mysql subquery temp-tables


【解决方案1】:

让我们稍微备份一下。因此,您可能有一个如下所示的中间结果集。从中,您想要哪些结果以及为什么?

SELECT d.*
     , w.wid
     , w.word 
  FROM worddates d 
  JOIN words w 
    ON w.did = d.did;
+-----+------------+-----+------+
| dID | wDate      | wid | word |
+-----+------------+-----+------+
|   1 | 2015-01-01 |   1 | dca  |
|   1 | 2015-01-01 |   2 | mno  |
|   1 | 2015-01-01 |   3 | xyz  |
|   1 | 2015-01-01 |   4 | abc  |
|   2 | 2015-01-02 |   5 | abc  |
|   2 | 2015-01-02 |   6 | dca  |
|   2 | 2015-01-02 |   7 | dad  |
|   2 | 2015-01-02 |   8 | dad  |
|   3 | 2015-01-03 |   9 | abc  |
|   3 | 2015-01-03 |  10 | abc  |
|   3 | 2015-01-03 |  11 | dca  |
|   3 | 2015-01-03 |  12 | bcd  |
|   4 | 2015-01-04 |  13 | xyz  |
|   4 | 2015-01-04 |  14 | abc  |
|   4 | 2015-01-04 |  15 | dad  |
|   4 | 2015-01-04 |  16 | dca  |
|   5 | 2015-01-05 |  17 | mno  |
|   5 | 2015-01-05 |  18 | xyz  |
|   5 | 2015-01-05 |  19 | def  |
|   5 | 2015-01-05 |  20 | mno  |
|   6 | 2015-01-06 |  21 | dca  |
|   6 | 2015-01-06 |  22 | bcd  |
|   6 | 2015-01-06 |  23 | dbd  |
|   6 | 2015-01-06 |  24 | dad  |
|   6 | 2015-01-06 |  25 | mno  |
|   1 | 2015-01-01 |  26 | dca  |
|   3 | 2015-01-03 |  27 | bcd  |
|   4 | 2015-01-04 |  28 | xyz  |
|   4 | 2015-01-04 |  29 | abc  |
|   4 | 2015-01-04 |  30 | abc  |
|   7 | 2015-01-07 |  31 | dac  |
|   4 | 2015-01-04 |  32 | abc  |
|   8 | 2015-01-08 |  33 | dad  |
|   9 | 2015-01-09 |  34 | dca  |
|   8 | 2015-01-08 |  35 | mno  |
|  11 | 2015-01-11 |  36 | xyz  |
|   8 | 2015-01-08 |  37 | def  |
|   9 | 2015-01-09 |  38 | hij  |
|  10 | 2015-01-10 |  39 | him  |
|  12 | 2015-01-12 |  40 | efg  |
|   9 | 2015-01-09 |  41 | and  |
|  11 | 2015-01-11 |  42 | dad  |
|  12 | 2015-01-12 |  43 | mno  |
|  10 | 2015-01-10 |  44 | dca  |
|   9 | 2015-01-09 |  45 | hij  |
|   7 | 2015-01-07 |  46 | def  |
|   7 | 2015-01-07 |  47 | efg  |
|   9 | 2015-01-09 |  48 | mno  |
+-----+------------+-----+------+

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    • 1970-01-01
    • 2012-11-27
    • 2015-12-07
    • 2011-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多