-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolls.sql
More file actions
26 lines (26 loc) · 782 Bytes
/
polls.sql
File metadata and controls
26 lines (26 loc) · 782 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
SELECT `id`,
`name`,
`winners`,
`created`,
`closed`,
COUNT(DISTINCT `user`) AS `ballots`
FROM (
SELECT `elections`.`id` AS `id`,
`elections`.`name` AS `name`,
`elections`.`winners` AS `winners`,
`elections`.`created` AS `created`,
`elections`.`closed` AS `closed`,
`votes`.`user`
FROM `elections`
LEFT JOIN `candidates` ON `elections`.`id` = `candidates`.`election`
LEFT JOIN `votes` ON `candidates`.`id` = `votes`.`candidate`
UNION SELECT `elections`.`id` AS `id`,
`elections`.`name` AS `name`,
`elections`.`winners` AS `winners`,
`elections`.`created` AS `created`,
`elections`.`closed` AS `closed`,
`writeins`.`user`
FROM `elections`
LEFT JOIN `writeins` ON `elections`.`id` = `writeins`.`election`
)
GROUP BY `id`