有3张桌子。
1-参与者==>可以让人们登录以参加抽奖的表
2-sorteado ==>保留获奖者的表
3-promocao ==>表,其中包含人们选择登录的地方的信息
当我进行抽奖时,我想选择仍不在sorteado
表中的获奖者(因为我可以进行其他抽奖)。
另外,我想首先选择分数较少的人,例如,在先前抽奖中尚未选择的人。
到目前为止,我有:
select
sorteado.*,
participante.*,
(select sum(pontos)
from sorteado
where id_participante = participante.id_participante
and validade >= now()
group by participante.id_usuario) as s
from sorteado
right join participante
on sorteado.id_participante = participante.id_participante
where id_promocao = 49
and sorteado.id_sorteado is null
order by s, rand();
s将是SUM。它显示为NULL,但是正确的值为3(对于id_usuario = 3)。
如果我更改查询,我将得到几乎我想要的东西。但是,仅当我通过id_usuario = 3时,该方法才有效。但是,我希望它适用于所有用户。
从sorteado中选择sorteado。,参加者。,(select sum(pontos) from sorteado where id_usuario = 3 and validade >= now() group by participante.id_usuario
)作为s,在sorteado.id_participante = particlee.id_participante上右加入参加者,其中id_promocao = 49,sorteado.id_sorteado为空顺序通过s,rand()
下面是数据库转储
--
-- Table structure for table `participante`
--
CREATE TABLE `participante` (
`id_participante` int(11) UNSIGNED NOT NULL,
`id_promocao` int(11) NOT NULL,
`id_usuario` int(11) UNSIGNED NOT NULL,
`inscricao` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `participante`
--
INSERT INTO `participante` (`id_participante`, `id_promocao`, `id_usuario`, `inscricao`) VALUES
(43, 50, 3, '2019-02-08 09:06:31'),
(44, 49, 3, '2019-02-08 09:07:01'),
(45, 49, 6, '2019-02-08 09:07:01'),
(46, 49, 7, '2019-02-08 09:07:01'),
(47, 49, 8, '2019-02-08 09:07:01');
-- --------------------------------------------------------
--
-- Table structure for table `promocao`
--
CREATE TABLE `promocao` (
`id_promocao` int(11) NOT NULL,
`nome` varchar(100) NOT NULL,
`descricao` text NOT NULL,
`cota` int(11) NOT NULL,
`ingressos_ganhador` int(11) NOT NULL,
`pontos` int(11) NOT NULL,
`prazo_pontos` int(11) NOT NULL,
`encerramento` date NOT NULL,
`mensagem` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `promocao`
--
INSERT INTO `promocao` (`id_promocao`, `nome`, `descricao`, `cota`, `ingressos_ganhador`, `pontos`, `prazo_pontos`, `encerramento`, `mensagem`) VALUES
(49, 'h2', '', 10, 1, 2, 30, '2019-09-14', ''),
(50, 'ppp', 'sdfsdf', 2, 2, 3, 10, '2019-02-07', ''),
(51, 'gfhjhgjgghjghjghj', '', 2, 2, 4, 10, '2019-02-07', '');
-- --------------------------------------------------------
--
-- Table structure for table `sorteado`
--
CREATE TABLE `sorteado` (
`id_sorteado` int(11) NOT NULL,
`id_participante` int(11) UNSIGNED NOT NULL,
`data_sorteio` datetime NOT NULL,
`pontos` int(11) NOT NULL,
`validade` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `sorteado`
--
INSERT INTO `sorteado` (`id_sorteado`, `id_participante`, `data_sorteio`, `pontos`, `validade`) VALUES
(1, 43, '2019-02-08 00:00:00', 3, '2019-02-15 00:00:00');
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE `users` (
`id` int(11) UNSIGNED NOT NULL,
`ip_address` varchar(45) NOT NULL,
`username` varchar(100) DEFAULT NULL,
`password` varchar(255) NOT NULL,
`salt` varchar(255) DEFAULT NULL,
`email` varchar(254) NOT NULL,
`activation_code` varchar(40) DEFAULT NULL,
`forgotten_password_code` varchar(40) DEFAULT NULL,
`forgotten_password_time` int(11) UNSIGNED DEFAULT NULL,
`remember_code` varchar(40) DEFAULT NULL,
`created_on` int(11) UNSIGNED NOT NULL,
`last_login` int(11) UNSIGNED DEFAULT NULL,
`active` tinyint(1) UNSIGNED DEFAULT NULL,
`first_name` varchar(50) DEFAULT NULL,
`last_name` varchar(50) DEFAULT NULL,
`company` varchar(100) DEFAULT NULL,
`phone` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `users`
--
INSERT INTO `users` (`id`, `ip_address`, `username`, `password`, `salt`, `email`, `activation_code`, `forgotten_password_code`, `forgotten_password_time`, `remember_code`, `created_on`, `last_login`, `active`, `first_name`, `last_name`, `company`, `phone`) VALUES
(3, '127.0.0.1', 'xx@xx.xx.xx', 'xxx', NULL, 'xxxxx@xx.xx.xx', NULL, NULL, NULL, 'xxx', 1549028455, 1549626215, 1, 'renan', NULL, NULL, NULL),
(4, '', 'a', '', NULL, 'email a\r\n', NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL),
(5, '', 'b', '', NULL, 'email b\r\n', NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL),
(6, '', 'c', '', NULL, 'email c', NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL),
(7, '', 'd', '', NULL, 'email d', NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL),
(8, '', 'e', '', NULL, 'email e', NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL),
(9, '', 'f', '', NULL, 'email f', NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL);
--
-- Indexes for dumped tables
--
--
-- Indexes for table `participante`
--
ALTER TABLE `participante`
ADD PRIMARY KEY (`id_participante`),
ADD UNIQUE KEY `unico` (`id_promocao`,`id_usuario`),
ADD KEY `id_promocao` (`id_promocao`),
ADD KEY `id_usuario` (`id_usuario`);
--
-- Indexes for table `promocao`
--
ALTER TABLE `promocao`
ADD PRIMARY KEY (`id_promocao`);
--
-- Indexes for table `sorteado`
--
ALTER TABLE `sorteado`
ADD PRIMARY KEY (`id_sorteado`),
ADD UNIQUE KEY `id_participante` (`id_participante`);
--
-- Indexes for table `users`
--
ALTER TABLE `users`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `participante`
--
ALTER TABLE `participante`
MODIFY `id_participante` int(11) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=48;
--
-- AUTO_INCREMENT for table `promocao`
--
ALTER TABLE `promocao`
MODIFY `id_promocao` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=52;
--
-- AUTO_INCREMENT for table `sorteado`
--
ALTER TABLE `sorteado`
MODIFY `id_sorteado` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
MODIFY `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=10;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `participante`
--
ALTER TABLE `participante`
ADD CONSTRAINT `fk_promocao` FOREIGN KEY (`id_promocao`) REFERENCES `promocao` (`id_promocao`),
ADD CONSTRAINT `fk_usuario` FOREIGN KEY (`id_usuario`) REFERENCES `users` (`id`);
--
-- Constraints for table `sorteado`
--
ALTER TABLE `sorteado`
ADD CONSTRAINT `fk_participante` FOREIGN KEY (`id_participante`) REFERENCES `participante` (`id_participante`);