【问题标题】:mysql limit with inner join and subquery带有内部连接和子查询的mysql限制
【发布时间】:2013-01-05 00:10:58
【问题描述】:

我有以下疑问:

SELECT saturday_combinations.index, v.val AS  `row` , COUNT( * )  AS  `count` 
FROM saturday_combinations  
INNER JOIN (

SELECT ONE AS val
FROM saturday_combinations 
WHERE ONE IS NOT NULL 

UNION 
SELECT TWO AS val
FROM saturday_combinations
WHERE TWO IS NOT NULL 

UNION 
SELECT THREE AS val
FROM saturday_combinations
WHERE THREE IS NOT NULL 

UNION 
SELECT FOUR AS val
FROM saturday_combinations
WHERE FOUR IS NOT NULL 

UNION 
SELECT FIVE AS val
FROM saturday_combinations
WHERE FIVE IS NOT NULL


UNION 
SELECT SIX AS val
FROM saturday_combinations
WHERE SIX IS NOT NULL 

UNION 
SELECT SEVEN AS val
FROM saturday_combinations
WHERE SEVEN IS NOT NULL 

) v  ON v.val = saturday_combinations.ONE
  OR v.val = saturday_combinations.TWO
  OR v.val = saturday_combinations.THREE
  OR v.val = saturday_combinations.FOUR
  OR v.val = saturday_combinations.FIVE
  OR v.val = saturday_combinations.SIX
  OR v.val = saturday_combinations.SEVEN 
  GROUP BY v.val 

查询的目的是提供表 saturday_combinations 中 ONE、TWO、THREE、FOUR、FIVE、SIX 和 SEVEN 列中包含的不同值的计数。但是我想设置一个 desc 限制 4 以便它只根据最后 4 行(最后四个最大索引)执行计数。但我没有让它与工会合作。在最后添加 order 和 limit 仅限制最终选择,而不是获取最后 4 行并计算它们的分布。有什么建议吗?

表架构如下:

index | ONE|TWO|THREE|FOUR|FIVE|SIX|SEVEN
 1       1   3   7     10    11  12  13
 2       3   4   5     30    31  22  23
 3       1   2   3      4     5   6   7
 4       1   2   3      4     5   6   7
 5       1   2   3      4     5   6   7
 6       1   2   3      4     5   6   7
 7       1   2   3      4     5   6   7
 8       1   2   3      4     5   6   7
 9       1   2   3      4     5   6   7
 10      1   2   3      4     5   6   7

索引是自动递增的,ONE-SEVEN 具有整数值。

表中有大约 3000 行,我想根据最后 n 行计算每个值的出现次数。

Ideal result for the last n rows where n = last 3 rows should be
Numbers|Count
   1       3
   2       3
   3       3
   4       3
   5       3
   6       3
   7       3

如果我增加 n 以包括最后 6 行,它们的计数应该会增加。如果我可以持续 10 行,那么计数应该会增加,并且其他数字应该与它们的计数一起出现。

这里是一个真实表格示例的链接。 http://sqlfiddle.com/#!2/d035b

【问题讨论】:

  • 请显示您的表格的表格架构和小样本数据。为什么不使用case statementcount (*)?澄清一下,您希望每个联合选择子查询按 desc 排序并限制为 4 个?
  • 嗨 bonCodigo,我已经编辑了包含更多信息的问题。
  • 忘了问,你能不能也展示一下理想的预期输出是多少? (样本基于您的样本表数据)请格式化code tags.
  • 我也包含了样本和理想结果。
  • 请查看更新。 +1 问题 :)

标签: mysql sql join limit


【解决方案1】:

如果对我的评论的回答是yes,那么您可以尝试以下方法。当您需要将limit, order by 添加到union selects 时,您需要用括号() 包裹union queries

代码:

(SELECT ONE AS val
FROM saturday_combinations 
WHERE ONE IS NOT NULL 
order by ONE desc limit 4)

UNION 
(SELECT TWO AS val
FROM saturday_combinations
WHERE TWO IS NOT NULL 
order by TWO desc limit 4)

UNION 
(SELECT THREE AS val
FROM saturday_combinations
WHERE THREE IS NOT NULL 
order by THREE desc limit 4)

如果对我的评论的回答是no,请澄清。

这是基于您的示例日期的代码:

select distinct x.one as uniqunumbers,
count(x.one) as counts
from(
sELECT DISTINCT 'one' 
AS col1, one FROM sat_comb
UNION ALL
SELECT DISTINCT 'two' 
AS col1, two FROM sat_comb
UNION ALL
SELECT DISTINCT 'three' 
AS col1, three FROM sat_comb
) as x
group by x.one;

UNIQUNUMBERS    COUNTS
1               1
3               2
4               1
5               1
7               1

根据 OP 编辑​​已澄清并更新了问题。

引用:“但是我想限制它,以便它首先获取最后 n 行,然后对这 n 行中的值进行计数。这意味着,如果我有 3 列,3000 行和 35整数随机出现在这 3000 行中,它应该计算每个整数出现的次数。”

查询:

select x.one as uniqunumbers,
    count(x.one) as counts
    from(
    (sELECT DISTINCT 'one' 
    AS col1, one FROM sat_comb
     order by id desc limit 4)
    UNION ALL
    (SELECT DISTINCT 'two' 
    AS col1, two FROM sat_comb
     order by id desc limit 4)
    UNION ALL
    (SELECT DISTINCT 'three' 
    AS col1, three FROM sat_comb
     order by id desc limit 4)
      UNION ALL
    (SELECT DISTINCT 'four' 
    AS col1, four FROM sat_comb
     order by id desc limit 4)
      UNION ALL
    (SELECT DISTINCT 'five' 
    AS col1, five FROM sat_comb
     order by id desc limit 4)
      UNION ALL
    (SELECT DISTINCT 'six' 
    AS col1, six FROM sat_comb
     order by id desc limit 4)
      UNION ALL
    (SELECT DISTINCT 'seven' 
    AS col1, seven FROM sat_comb
     order by id desc limit 4)
    ) as x
    group by x.one;

输出:

UNIQUNUMBERS    COUNTS
2               4
3               3
4               3
5               4
6               4
8               3
9               4
20              3

【讨论】:

  • 在您更新的查询中,我可以在哪里添加限制语法?即使有括号,查询也不会产生正确的结果。
  • 我运行了您的独特查询,但没有完全产生我所期望的结果。它似乎给出了完全不同的计数。
  • @SoucianceEqdamRashti 你可能会注意到 Bluefeet 和我的那种有相同的解决方案!我的和他的一样简单;)顺便说一句unpivot 也是sql server 中的一个函数。但是整个union 过程称为unpivoting。无论如何你能确认一下你想精确限制哪些数据吗?你想从每列中获取最多 4 个不同的数字吗?至于结果,你看到我附上的DEMO了吗。请注意,我只使用前 3 列作为示例。
  • 这是我从你的数据中得到的,请检查sqlfiddle BTW 我没有看到你的任何查询,但创建/插入。仅供参考:我根据我的原始查询和您的示例使用了limit by 4。如果一切顺利,请分享。
  • @SoucianceEqdamRashti 请查看您提供给我的sqlfiddle。您可以删除第一个distinct
【解决方案2】:

也许我在您的请求中遗漏了一些东西,但根据您想要的结果,为什么不直接unpivot 数据并执行计数。

select value, count(*) Total
from
(
  select 'one' col, one value
  from saturday_combinations
  union all
  select 'two' col, two value
  from saturday_combinations
  union all
  select 'three' col, three value
  from saturday_combinations
  union all
  select 'four' col, four value
  from saturday_combinations
  union all
  select 'five' col, five value
  from saturday_combinations
  union all
  select 'six' col, six value
  from saturday_combinations
  union all
  select 'seven' col, seven value
  from saturday_combinations
) src
group by value

SQL Fiddle with Demo

您的样本结果是:

| VALUE | TOTAL |
-----------------
|     1 |     1 |
|     3 |     2 |
|     4 |     1 |
|     5 |     1 |
|     7 |     1 |
|    10 |     1 |
|    11 |     1 |
|    12 |     1 |
|    13 |     1 |
|    22 |     1 |
|    23 |     1 |
|    30 |     1 |
|    31 |     1 |

编辑 #1:根据您的更新,这可能需要您什么:

select value, count(*)
from
(
  select col, value
  from
  (
    select 'one' col, one value
    from saturday_combinations
    order by one 
    limit 3
  ) one
  union all
  select col, value
  from
  (
    select 'two' col, two value
    from saturday_combinations
    order by two desc
    limit 3
  ) two
  union all
  select col, value
  from
  (
    select 'three' col, three value
    from saturday_combinations
    order by three 
    limit 3
  ) three
  union all
  select col, value
  from
  (
    select 'four' col, four value
    from saturday_combinations
    order by four 
    limit 3
  ) four
  union all
  select col, value
  from
  (
    select 'five' col, five value
    from saturday_combinations
    order by five 
    limit 3
  ) five
  union all
  select col, value
  from
  (
    select 'six' col, six value
    from saturday_combinations
    order by six 
    limit 3
  ) six
  union all
  select col, value
  from
  (
    select 'seven' col, seven value
    from saturday_combinations
    order by seven 
    limit 3
  ) seven
) src
group by value

SQL Fiddle with Demo

结果:

| VALUE | COUNT(*) |
--------------------
|     1 |        3 |
|     2 |        1 |
|     3 |        4 |
|     4 |        4 |
|     5 |        3 |
|     6 |        3 |
|     7 |        3 |

【讨论】:

  • +1 用于更简单的查询,但我可以在哪里设置 desc 限制以仅从最后 n 行开始计数?
  • @SoucianceEqdamRashti 我不确定您要限制什么。您可以使用显示限制内容的示例更新您的 OP 吗?
【解决方案3】:

如果您想查看最终解决方案: http://sqlfiddle.com/#!2/867a6/13 感谢@bonCodigo 的所有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-07
    相关资源
    最近更新 更多