2016-03-27 23 views
0

Senaryo: Kullanıcı başına günde 100 öğeyi işlemek için bir öğe sırası.Birleştirme öğesi, hesaplanan alan değerlerini bile atlar

Açıklama: Burada, 24 saat boyunca işlenmemiş geçmiş günlük kotasına sahip işlenmemiş öğelerle işlenen rekor birleşiktir.

SELECT 
    q.*, 
    case when delivery.processed is null then 0 else delivery.processed end as processed, 
    @subcounter := IF(@context = q.user_id, @subcounter+1, 1) as counter, 
    @context := q.user_id as uid 
from queue q 

LEFT JOIN (
    SELECT `user_id`, count(*) as `processed` FROM `queue` 
    where `status` = 'processed' and `updated_at` >= DATE_SUB(NOW(), INTERVAL 24 HOUR) 
    GROUP BY `user_id`) as `delivery` 
    ON `delivery`.`user_id` = `q`.`user_id` 

WHERE 
    `q`.`status` = 'queued' and 
    (`delivery`.`processed` is null or `delivery`.`processed` < 100) 
HAVING 
    `counter` <= (100 - `delivery`.`processed`) 

son durum counter <= (100 - delivery.processed) sayaç bile nerede alternatif her satır atlamak için hatalı olduğunu. (2 Son sütun nedeniyle tüm eşitler numaralarını atlayarak, diziyi eksik: Ben atlayamazsınız bu son durumun olmadan

kaydeden olanlar İşte 100.

Table: queue 
Columns: 
id bigint(20) UN AI PK 
created_at timestamp 
updated_at timestamp 
job_id bigint(20) 
user_id bigint(20) 
status varchar(64) 

günlük kota sınırını aşan olacak sorgu sonucudur ne var etrafında ve ekstra SELECT - fıkra koşulu)

'224244', '2016-03-24 22:44:32', '2016-03-24 22:44:32', '23942', '3', 'queued', '0', '1', '3' 
'224995', '2016-03-24 22:45:25', '2016-03-24 22:45:25', '23963', '3', 'queued', '0', '3', '3' 
'225208', '2016-03-24 22:45:40', '2016-03-24 22:45:40', '23970', '3', 'queued', '0', '5', '3' 
'225316', '2016-03-24 22:45:47', '2016-03-24 22:45:47', '23972', '3', 'queued', '0', '7', '3' 
'227247', '2016-03-27 15:17:00', '2016-03-27 15:17:00', '22741', '3', 'queued', '0', '9', '3' 
'227258', '2016-03-27 15:17:00', '2016-03-27 15:17:00', '22747', '3', 'queued', '0', '11', '3' 
'228291', '2016-03-27 15:18:22', '2016-03-27 15:18:22', '24537', '3', 'queued', '0', '13', '3' 

cevap

0

sahip sana fazladan bir katman gerek. Dışında, HAVING'u en dışta hareket ettirin, bu noktada WHERE olarak değiştirebilirsiniz.

İlgili konular