我有以下查询,当我运行它时会给我错误,
SELECT events.id AS id, SUM(tickets_sold.quantity)
FROM (`events`)
JOIN `category` AS cat ON `events`.`category_id` = `cat`.`id`
JOIN `category` AS sub_cat ON `events`.`subCategoryID` = `sub_cat`.`id`
JOIN `events_custom_dates` AS events_date ON `events_date`.`event_id` = `events`.`id`
JOIN `my_promos` ON `events`.id = `my_promos`.`event_id`
LEFT JOIN `mycalendar` ON `mycalendar`.`event_id` = `my_promos`.`event_id`
LEFT JOIN `promo_events_stats` ON `promo_events_stats`.`id` = `events`.`id`
JOIN `tickets_sold` ON `my_promos`.`link_code` = `tickets_sold`.`code`
WHERE `my_promos`.`user_id` = '532' AND DATE(my_promos.date) >= '2013-11-01' AND DATE(my_promos.date) <= '2014-01-22'
GROUP BY my_promos.event_id
它给了我
id sum(tickets.sold)
some id `2`
some id `4`
some id `14`
但应该给出
some id `2`
some id `4`
some id `7`
当我从上面的查询中删除以下行时,它为我提供了正确的数据
JOIN `events_custom_dates` AS events_date ON `events_date`.`event_id` = `events`.`id`
所以请告诉我现在应该怎么做.. event_id是我可以与事件表联系的唯一外键。
答案 0 :(得分:1)
这是您的需求解决方案
SELECT events.id AS id,A.sold_tickets
FROM (`events`)
JOIN `category` AS cat ON `events`.`category_id` = `cat`.`id`
JOIN `category` AS sub_cat ON `events`.`subCategoryID` = `sub_cat`.`id`
JOIN `events_custom_dates` AS events_date ON `events_date`.`event_id` = `events`.`id`
JOIN `my_promos` ON `events`.id = `my_promos`.`event_id`
LEFT JOIN `mycalendar` ON `mycalendar`.`event_id` = `my_promos`.`event_id`
LEFT JOIN `promo_events_stats` ON `promo_events_stats`.`id` = `events`.`id`
LEFT JOIN (SELECT my_promos.event_id, SUM(tickets_sold.quantity) AS sold_tickets FROM my_promos
JOIN tickets_sold ON tickets_sold.code = my_promos.link_code
WHERE my_promos.user_id = '532' AND DATE(my_promos.date) >= '2013-11-01' AND DATE(my_promos.date) <= '2014-01-22'
GROUP BY my_promos.event_id) A ON A.event_id = events.id
WHERE `my_promos`.`user_id` = '532' AND DATE(my_promos.date) >= '2013-11-01' AND DATE(my_promos.date) <= '2014-01-22'
GROUP BY my_promos.event_id
答案 1 :(得分:0)
确实
select *
from events
join events_custom_dates as events_date on events_date.event_id = events.id;
给出你期望的结果?我希望架构可能存在问题,但是无法看到它,这在黑暗中有点刺痛。