簡體   English   中英

MySQL 計算列值相同的行,並選擇計數大於 2 的行

[英]MySQL count rows where column value is same and select them where count is greater than 2

我正在處理一個我必須回溯的問題,因為整個項目已經投入生產並且系統已經使用了一段時間。

我需要使用以下參數回溯所有數據。

Select * from table where bundle = 5 and count(bundle) >= 3

這將是一個連接表,因此從技術上講,我需要具有相同事務的包數大於 2。

例如

id | transaction | bundle
-------------------------
1  | 123         | 5
3  | 234         | 15
12 | 1111        | 5
13 | 1111        | 15
17 | 1111        | 5
18 | 1111        | 5

到目前為止我的代碼

select * from table_i as ti
right join table_r as tr
on tr.id = ti.t_id
where ti.type_id = x and ti.bundle = 5 and ti.online = 1 and count(ti.bundle) >=5

謝謝

編輯真實代碼:

SELECT ti.*, tr.*
FROM ticket_items AS ti
INNER join transactions as tr
ON tr.id = ti.trans_id
INNER JOIN
(
    SELECT tis.trans_id, COUNT(tis.bundle) AS bundle_count
    FROM ticket_items as tis
    INNER join transactions as trs
    ON trs.id = tis.trans_id
    WHERE tis.type_id = 2
    AND tis.bundle = 5 
    AND tis.online = 1 
    HAVING bundle_count > 2
) sub0
ON sub0.trans_id = ti.trans_id
WHERE ti.type_id = 2
AND ti.bundle = 5 
AND ti.online = 1 

結果: 1328 1 1 766 2 5 25 1 1 2015-10-26 20:26:41 2015-10-27 00:00:02 0 766 1 0 John Doe 123-123-1234 NULL email@email.com NULL NULL NULL NULL 1 164 Cedar Square NULL 123 rrt city province country 125 2015-10-26 20:26:41 2015-10-26 20:26:41 125.00 0.00 0.00 0.00 0 1

表ticket_items:

id | lot | system | trans_id | type | bundle | price | print | online | date                | update              | void
1    1     2        1          1      1        100     1       0        2015-10-01 23:30:12   2015-10-03 18:49:25   0
2    1     2        1          2      15       50      1       0        2015-10-01 23:30:12   2015-10-03 16:48:15   0
3    1     3        2          1      1        100     1       0        2015-10-02 00:13:57   2015-10-02 00:22:17   1
4    1     3        2          2      15       50      1       0        2015-10-02 00:13:57   2015-10-02 00:19:17   1

表交易:

id | lot_id | cust | first | last| number |||||||
1  | 1      | 23   | john  | doe | 123

我對你問題的措辭有點困惑,但試試這個:

SELECT ti.id, ti.transaction, count(ti.bundle)
FROM table_i as ti
JOIN table_r as tr
  ON tr.id = ti.id
WHERE ti.type_id = x 
  AND ti.bundle = 5 
  AND ti.online = 1 
  AND count(ti.bundle) >=5

我認為你需要使用having 像這樣的東西:

select ti.*, count(ti.bundle) from table_i as ti
right join table_r as tr
on tr.id = ti.t_id
where ti.type_id = x and ti.bundle = 5 and ti.online = 1
group by ti.bundle
having count(ti.bundle) >=5; /* or >= 3 depending on which you're using now*/

如果我理解正確,您想找到超過 3 個的交易,其中交易代碼為 5 個且 online = 1,然后從 2 個表中帶回該交易的所有行,交易量為 5和在線 = 1

如果是這樣,子查詢以獲取計數超過 3 的事務,然后將其連接回 2 個表:-

SELECT ti.*, tr.*
FROM table_i AS ti
INNER join table_r as tr
ON tr.id = ti.t_id
INNER JOIN
(
    SELECT tis.transaction, COUNT(tis.bundle) AS bundle_count
    FROM table_i as tis
    INNER join table_r as trs
    ON trs.id = tis.t_id
    WHERE tis.type_id = x 
    AND tis.bundle = 5 
    AND tis.online = 1 
    GROUP BY tis.transaction
    HAVING bundle_count >= 3
) sub0
ON sub0.transaction = ti.transaction
WHERE ti.type_id = x 
AND ti.bundle = 5 
AND ti.online = 1 

暫無
暫無

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

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