Last active
July 17, 2017 14:21
-
-
Save tihoho/af74e968a254dbb3f5d42804adbb5ec4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- юзеры | |
CREATE TABLE `users` ( | |
`id` INT(11) NOT NULL AUTO_INCREMENT, | |
`firstname` VARCHAR(32) NULL DEFAULT 'No_name', | |
PRIMARY KEY (`id`) | |
) | |
ENGINE=InnoDB | |
; | |
-- хобби | |
CREATE TABLE `hobbies` ( | |
`id` INT(11) NOT NULL AUTO_INCREMENT, | |
`title` VARCHAR(50) NOT NULL, | |
PRIMARY KEY (`id`), | |
INDEX `title` (`title`) | |
) | |
ENGINE=InnoDB | |
; | |
-- юзеры → хобби | |
CREATE TABLE `users_hobbies` ( | |
`user_id` INT(10) UNSIGNED NOT NULL, | |
`hobby_id` SMALLINT(5) UNSIGNED NOT NULL, | |
UNIQUE INDEX `users_hobbies` (`user_id`, `hobby_id`) | |
) | |
ENGINE=InnoDB | |
; | |
-- топ хобби | |
select | |
uh.hobby_id, | |
h.title 'hobby_title', | |
count(uh.hobby_id) `cnt` | |
from | |
users_hobbies uh | |
left join | |
hobbies h on uh.hobby_id = h.id | |
group by | |
uh.hobby_id | |
order by | |
`cnt` desc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment