简体   繁体   English

内部联接和子查询的mysql限制

[英]mysql limit with inner join and subquery

I have the following query: 我有以下查询:

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 

The purpose of the query is to provide a count of the different values contained in the columns ONE,TWO,THREE,FOUR,FIVE,SIX and SEVEN in the table saturday_combinations. 查询的目的是提供表saturday_combinations中列ONE,TWO,THREE,FOUR,FIVE,SIX和SEVEN中包含的不同值的计数。 However I want to put a desc limit 4 so that it only performs the count based on the last 4 rows (last four maximum indexes). 但是我想设置一个desc限制4,以便它只根据最后4行(最后4个最大索引)执行计数。 But I am not getting it to work with the union. 但我没有让它与工会合作。 Adding order and limit at the very end only limits from the final select, rather than get the last 4 rows and calculate the distribution on them. 最后添加顺序和限制仅限于最终选择,而不是获取最后4行并计算它们的分布。 Any tips? 有小费吗?

The table schema is as follows: 表模式如下:

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

Index is auto-increment and ONE-SEVEN has integer values. 索引是自动递增,ONE-SEVEN具有整数值。

There are about 3000 rows in the table and I want to count occurences for each value based on the last n rows. 表中大约有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

If I increase n to include last 6 rows their count should increase. 如果我增加n以包括最后6行,则其计数应该增加。 If I could last 10 rows the count should increase and other numbers should appear with their count. 如果我可以持续10行,则计数应该增加,其他数字应该随着计数而出现。

Here is a link to a sample of the real table. 这是一个真实表样本的链接。 http://sqlfiddle.com/#!2/d035b http://sqlfiddle.com/#!2/d035b

If answer to my comment is yes then, you could try the following. 如果回答我的评论是yes那么您可以尝试以下方法。 When you need to add limit, order by to union selects you need to wrap union queries with brackets () . 当你需要添加limit, order by union selects你需要用括号()包装union queries

Code: 码:

(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)

If answer to my comment is no , then please clarify. 如果对我的评论的回答是no ,那么请澄清。

Here is the code based on your sample date: 以下是基于您的样本日期的代码:

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

EDIT as per OP has clarified and updated the question. 根据OP编辑澄清并更新了问题。

Quoted: "However I want to limit it so that it first takes the last n rows and then does the count on the values in those n rows. This means, if I have 3 columns with 3000 rows and 35 integers randomly appearing in these 3000 rows it should count how many times each integer appears." 引用: “但是我想限制它,以便它首先占用最后n行,然后对这n行中的值进行计数。这意味着,如果我有3列,其中3000行和35个整数随机出现在这3000行中它应该计算每个整数出现的次数。“

Query: 查询:

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;

Output: 输出:

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

Maybe I am missing something in your request but based on your desired result, why not just unpivot the data and perform the count. 也许我在你的请求中遗漏了一些东西,但根据你想要的结果,为什么不只是取消数据并执行计数。

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

See SQL Fiddle with Demo 请参阅SQL Fiddle with Demo

The result of your sample is: 您的样本的结果是:

| 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 |

Edit #1: Based on your update this might be want you what: 编辑#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

See SQL Fiddle with Demo 请参阅SQL Fiddle with Demo

Result: 结果:

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

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM