1
所以,我對於銷售門票的活動服務三個表:是否需要在這裏嵌套兩個COUNT?
Event(id)
Ticket(id, venue_id, seller_id)
Venue(id, event_id)
Seller(id)
而且我想檢測,每一個事件,我有多少賣家(1型)有有隻有售票(並且該票是相應事件)。像
Event_id; First_time_sellers
2814;3
3092;24
124;1
...
我有兩個疑問這個企圖,都未能得到錯誤的結果
SELECT event_id,
COUNT(DISTINCT rookie_seller)
FROM
(SELECT event_id,
rookie_seller,
tickets
FROM
(SELECT DISTINCT event.id AS event_id,
seller.id AS rookie_seller,
COUNT(DISTINCT ticket.id) AS tickets
FROM ticket
JOIN venue ON ticket.venue_id = venue.id
JOIN event ON venue.event_id = event.id
JOIN seller ON ticket.seller_id = seller.id
WHERE seller.type = 1
AND ticket.isforsale = 1
GROUP BY rookie_seller) a
WHERE tickets = 1) b
GROUP BY event_id
和
SELECT event.id,
a.seller_id,
a.tickets
FROM ticket
JOIN (
SELECT seller.id AS seller_id,
COUNT(DISTINCT ticket.id) AS tickets
FROM ticket
JOIN seller ON ticket.seller_id = seller.id
WHERE seller.type = 1
AND ticket.isforsale = 1
GROUP BY seller_id) a ON ticket.seller_id = a.seller_id
JOIN venue ON ticket.venue_id = venue.id
JOIN event ON venue.event_id = event.id
WHERE a.tickets = 1
我真的不知道是什麼問題。加入應該沒問題,還有計數條件,對不對?
感謝您的任何忠告:)
請提供您的樣本數據,您得到的結果和預期的數據 – Viki888