所以,我很难获得一个查询来显示具有不同类别的民意调查中的大多数投票的选项。目标是让site_polls_votes中出现最多的vote_id,并在site_polls中按cat_id对其进行分组,以便在site_polls_cat中仅显示每个类别的最多一个投票值,即9。
然而,奇怪的是,查询不是从site_polls返回正确的数据,而是返回我不想要的东西。
这里是DDL ......
CREATE TABLE IF NOT EXISTS `site_polls` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`cat_id` int(11) NOT NULL,
`value` varchar(255) NOT NULL,
`icon_url` varchar(255) DEFAULT NULL,
`radio_id` int(11) NOT NULL,
`status` enum('0','1') NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
KEY `fs_id` (`radio_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=75 ;
INSERT INTO `site_polls` (`id`, `cat_id`, `value`, `icon_url`, `radio_id`, `status`) VALUES
(1, 1, 'Navvid', 'hd.png', 1, '1'),
(2, 1, 'NavvoNight', 'f13c3b96c52ae8bd69a1202f703f9ba0.png', 2, '1'),
(3, 1, 'ProNavvo', '39fd904f5510a2a9e5a75892e54ffc4b.png', 3, '1'),
(4, 1, 'IceNavvo', '9488c40ba45ed8175e61cdc684701d98.png', 5, '1'),
(5, 1, 'Navvoz', '627165967747d031cbca7d31d778400e.png', 9, '1'),
(6, 1, 'Navvlindados', 'ff3f8a271623a9bd34f15af2b9766fe3.png', 12, '1'),
(7, 1, 'BeatsNavvo', '71b789e9dd822d4e86ee76a74917cb6c.png', 14, '1'),
(8, 2, 'Arequis.', NULL, 5, '1'),
(9, 2, '.Hiroshima', NULL, 5, '1'),
(10, 2, 'Lyferhd', NULL, 5, '1'),
(11, 2, 'Cold.-.', NULL, 5, '1'),
(12, 2, 'Styamban', NULL, 5, '1'),
(13, 3, 'Lyps', NULL, 5, '1'),
(14, 3, 'bolaudozinho', NULL, 5, '1'),
(15, 3, 'Sr.Donatella', NULL, 5, '1'),
(16, 3, ':Stefanie0103', NULL, 5, '1'),
(17, 3, 'X-DesconhecidoX', NULL, 5, '1'),
(18, 4, 'wilsNavvoz', NULL, 5, '1'),
(19, 4, 'Incapaciidade', NULL, 5, '1'),
(20, 4, 'bruno64681', NULL, 5, '1'),
(21, 4, '=O=Marlon=O=', NULL, 5, '1'),
(22, 4, '-lordzika-', NULL, 5, '1'),
(23, 5, 'Incapaciidade', NULL, 5, '1'),
(24, 5, 'Lyferhd', NULL, 5, '1'),
(25, 5, 'wilsNavvoz', NULL, 5, '1'),
(26, 5, 'bolaudozinho', NULL, 5, '1'),
(27, 5, 'Dj.samuel.....', NULL, 5, '1'),
(28, 6, 'bruno64681', NULL, 5, '1'),
(29, 6, 'Incapaciidade', NULL, 5, '1'),
(30, 6, '.:Mariio:.', NULL, 5, '1'),
(31, 6, 'l.modric10', NULL, 5, '1'),
(32, 7, 'Lyps', NULL, 5, '1'),
(33, 7, 'Incapaciidade', NULL, 5, '1'),
(34, 7, '.:Mariio:.', NULL, 5, '1'),
(35, 7, '-lordzika-', NULL, 5, '1'),
(36, 7, 'SuperGrandChase2', NULL, 5, '1'),
(37, 8, '.:SurfistAa:.', NULL, 5, '1'),
(38, 8, 'Nelpsbreaker', NULL, 5, '1'),
(39, 8, 'Lyps', NULL, 5, '1'),
(40, 2, 'guilherme58512', NULL, 14, '1'),
(41, 2, 'erick.BAN10', NULL, 14, '1'),
(42, 2, 'Srto.GeetNavvo', NULL, 14, '1'),
(43, 2, 'Fients', NULL, 14, '1'),
(44, 3, 'HannahMello', NULL, 14, '1'),
(45, 3, 'DjAtualizado', NULL, 14, '1'),
(46, 3, '-TheusAlves.Ban', NULL, 14, '1'),
(47, 3, 'Pokadork', NULL, 14, '1'),
(48, 3, 'Dockepand', NULL, 14, '1'),
(49, 4, 'mavsonhc', NULL, 14, '1'),
(50, 4, 'TheMatencio', NULL, 14, '1'),
(51, 4, 'Dioguiiitoo', NULL, 14, '1'),
(52, 4, ',MatheuSinho10', NULL, 14, '1'),
(53, 5, 'Deboxadooanjo', NULL, 14, '1'),
(54, 5, 'Loranny.:', '', 14, '1'),
(55, 5, 'luizfernan4', NULL, 14, '1'),
(56, 5, 'lucasvoa', NULL, 14, '1'),
(57, 5, '--Djdana09--', NULL, 14, '1'),
(58, 6, 'luidygomesf', NULL, 14, '1'),
(59, 6, 'Domgabriel12365', NULL, 14, '1'),
(60, 6, 'Tac@-Fogo', NULL, 14, '1'),
(61, 6, 'stephanyeclub', NULL, 14, '1'),
(62, 6, 'Ops.Luuh', NULL, 14, '1'),
(63, 7, 'Dioguiiitoo', NULL, 14, '1'),
(64, 7, ',Yakaha', NULL, 14, '1'),
(65, 7, 'Dockepand', NULL, 14, '1'),
(66, 7, 'HannahMello', NULL, 14, '1'),
(67, 8, 'iDescreto', NULL, 5, '1'),
(68, 8, 'KevinK.CRS', NULL, 14, '1'),
(69, 8, 'samusan45', NULL, 14, '1'),
(70, 8, 'HannahMello', NULL, 14, '1'),
(71, 8, 'L3G4CY', NULL, 14, '1'),
(72, 9, '.Misteriow', NULL, 14, '1'),
(73, 9, 'Piresmobi2', NULL, 14, '1'),
(74, 9, 'Nadson.Oculto', NULL, 14, '1');
CREATE TABLE IF NOT EXISTS `site_polls_cat` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`icon` enum('0','1') NOT NULL DEFAULT '0',
`status` enum('0','1') NOT NULL DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;
INSERT INTO `site_polls_cat` (`id`, `title`, `icon`, `status`) VALUES
(1, 'Radio', '1', '1'),
(2, 'Webmaster', '0', '1'),
(3, 'Admini', '0', '1'),
(4, 'Coord', '0', '1'),
(5, 'Dj', '0', '1'),
(6, 'Moderator', '0', '1'),
(7, 'Press', '0', '1'),
(8, 'Promoter', '0', '1'),
(9, 'Artist', '0', '1');
CREATE TABLE IF NOT EXISTS `site_polls_votes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`vote_id` int(11) NOT NULL,
`data` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `vote_id` (`vote_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;
INSERT INTO `site_polls_votes` (`id`, `user_id`, `vote_id`, `data`) VALUES
(1, 2961, 2, 1481698602),
(2, 923, 2, 1481698679),
(3, 174, 5, 1481705971),
(4, 174, 8, 1481748108),
(5, 174, 15, 1481748132),
(6, 174, 22, 1481748153),
(7, 174, 36, 1481748188),
(8, 2961, 8, 1481794986),
(9, 2961, 45, 1481803289);
CREATE TABLE IF NOT EXISTS `site_radios` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`ip` varchar(255) NOT NULL,
`icon` varchar(255) NOT NULL,
`color` varchar(255) NOT NULL,
`status` enum('0','1') NOT NULL DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=18 ;
INSERT INTO `site_radios` (`id`, `name`, `ip`, `icon`, `color`, `status`) VALUES
(1, 'Navvid', '', 'Navvid.png', '#1777BC', '1'),
(2, 'NavvoNight', '', 'NavvoNight.png', '#3F1028', '1'),
(3, 'ProNavvo', '', 'ProNavvo.png', '#79BEDF', '1'),
(4, 'Pixelados', '', 'Pixelados.png', '#F0CA24', '0'),
(5, 'IceNavvo', '', 'IceNavvo.png', '#28ADFF', '1'),
(6, 'Navvonautas', '', 'Navvonautas.png', '#607391', '0'),
(7, 'Navvonados', '', 'Navvonados.png', '#93D4FF', '0'),
(8, 'KiNavvo', '', 'KiNavvo.png', '#308F95', '0'),
(9, 'Navvoz', '', 'Navvoz.png', '#C96547', '1'),
(10, 'Navvoleiros', '', 'Navvoleiros.png', '#8AC206', '0'),
(11, 'HBFM', '', '../dfff358a8030a4314ba1b0988a7251d4.png', '#0962BB', '0'),
(12, 'Navvlindados', '', '../58e2b864131ec83c84dca257c13874d0.png', '#F4BD49', '1'),
(13, 'ColdNavvo', '', '../66e217354c04a3d909ccd3c0efd20f8a.png', '#3A9BD1', '0'),
(14, 'BeatsNavvo', '', '../c28f753683e959c512e9ff7bf659635b.png', '#66B7DB', '1'),
(15, 'Nobba', '', '../c7029ca90687bc4a531fd780599eb085.png', '#FFCC4D', '0'),
(16, 'ConectionFM', '', '../537b5ae135bc7fc17d37991fbf40ec40.png', '#E44549', '1'),
(17, 'NavvoColor', '', '../88106d7614c550bdd9014e0d5948a3ab.png', '#D20052', '1');
我已在SQLFiddle中重现了该问题,请点击here。
如果SQLFiddle不适合你,这里是查询:
SELECT MAX(t.totalvotes) AS votes,
t.value,
t.cat_id,
t.radio_id,
t.icon_url,
t.title,
t.icon_radio
FROM
(SELECT COALESCE(COUNT(DISTINCT(v.id)),0) AS totalvotes,
e.value,
e.cat_id,
e.radio_id,
e.icon_url,
c.title,
r.icon AS icon_radio
FROM site_polls_votes v
LEFT OUTER JOIN site_polls e ON e.id = v.vote_id
INNER JOIN site_polls_cat c ON e.cat_id = c.id
INNER JOIN site_radios r ON e.radio_id = r.id
GROUP BY e.cat_id) t
GROUP BY t.cat_id
ORDER BY rand()
结果: LEFT OUTER JOIN http://image.prntscr.com/image/f65d6ea9ecbf4763bd115dde34fbaf2a.png
期望的结果应该是2票,#34; NavvoNight"和#34; DjAtualizado"。我认为问题在于查询是对cat_id的所有投票进行求和并将其归因于获胜者,使其仍然是投票最多的,但根据site_polls_votes显示错误的投票数。
还需要:如果没有人对特定类别进行投票,它也显示0票的方式,因此查询将始终显示基于类别数量的结果数量,就像我之前所说的那样,是9我设法得到了这个"有点"工作,但显示绝对错误的赢家,将LEFT OUTER JOIN部分更改为RIGHT OUTER JOIN。见这里:
RIGHT OUTER JOIN http://image.prntscr.com/image/fd896e47fb264c8d9130640914eb4ddc.png
任何帮助都将受到高度赞赏,提前谢谢!
答案 0 :(得分:0)
FROM SCRATCH
试试这个:
select
IF(a.totalvotes > 0, a.totalvotes, 0) as totalvotes,
IF(a.totalvotes > 0,b.value, c.value),
IF(a.totalvotes > 0,b.cat_id, c.cat_id),
IF(a.totalvotes > 0,b.radio_id, c.radio_id),
IF(a.totalvotes > 0,b.icon_url, c.icon_url),
a.title
from
(select e.id, count(distinct(v.id)) as totalvotes, e.title
from
site_polls_cat as e
left join site_polls as s ON (e.id = s.cat_id)
left join site_polls_votes as v ON (s.id = v.vote_id)
group by e.title ) as a
left join
(select s.id, s.value, s.cat_id, s.radio_id, s.icon_url, count(distinct(v.id)) as sitevotes
from
(site_polls as s, site_polls_votes as v)
left join site_radios as r ON (s.radio_id = r.id)
where
s.id = v.vote_id
group by
s.id, s.cat_id
order by sitevotes DESC) as b on (a.id = b.cat_id)
left join
(select s.id, s.value, s.cat_id, s.radio_id, s.icon_url
from
(site_polls as s, site_polls_cat as e)
left join site_polls_votes as v on (s.id = v.vote_id)
where
s.cat_id = e.id
&& v.vote_id is null
group by
s.cat_id, s.id
order by
RAND()) as c on (a.id = c.cat_id)
group by
a.id
order by RAND()