Skip to content

Instantly share code, notes, and snippets.

@tihoho
Last active July 17, 2017 14:21
Show Gist options
  • Save tihoho/af74e968a254dbb3f5d42804adbb5ec4 to your computer and use it in GitHub Desktop.
Save tihoho/af74e968a254dbb3f5d42804adbb5ec4 to your computer and use it in GitHub Desktop.
-- юзеры
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