簡體   English   中英

內部聯接和子查詢的mysql限制

[英]mysql limit with inner join and subquery

我有以下查詢:

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行(最后4個最大索引)執行計數。 但我沒有讓它與工會合作。 最后添加順序和限制僅限於最終選擇,而不是獲取最后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

如果回答我的評論是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

也許我在你的請求中遺漏了一些東西,但根據你想要的結果,為什么不只是取消數據並執行計數。

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 |

如果您想查看最終解決方案: http ://sqlfiddle.com/#!2/867a6/13感謝@bonCodigo的所有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM