我已经与这个查询作斗争了一天多。我的 SQL 并不神奇,所以这可以解释为什么!无论如何,如果有人能澄清我在这里做错了什么,我将不胜感激。
CREATE TABLE `business` (
`business_id` int(11) NOT NULL AUTO_INCREMENT,
)
CREATE TABLE `business_unit` (
`business_unit_id` int(11) NOT NULL AUTO_INCREMENT,
`business_id` int(11) NOT NULL,
)
CREATE TABLE `offer` (
`offer_id` int(11) NOT NULL AUTO_INCREMENT,
`business_unit_id` int(11) NOT NULL,
`points_required` int(11) NOT NULL,
)
CREATE TABLE `points_balance` (
`points_balance_id` int(11) NOT NULL AUTO_INCREMENT,
`mobile_user_id` int(11) NOT NULL,
`business_unit_id` int(11) NOT NULL,
`points` int(11) NOT NULL DEFAULT '0',
`record_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
)
数据将是:
INSERT INTO `business` (`business_id`) VALUES (1);
INSERT INTO `business` (`business_id`) VALUES (2);
INSERT INTO `business_unit` (`business_unit_id`,`business_id`) VALUES (11, 1);
INSERT INTO `business_unit` (`business_unit_id`,`business_id`) VALUES (12, 1);
INSERT INTO `business_unit` (`business_unit_id`,`business_id`) VALUES (13, 2);
INSERT INTO `business_unit` (`business_unit_id`,`business_id`) VALUES (14, 2);
INSERT INTO `offer` (`offer_id`,`business_unit_id`,`points_required`) VALUES (21, 11, 50);
INSERT INTO `offer` (`offer_id`,`business_unit_id`,`points_required`) VALUES (22, 12, 50);
INSERT INTO `offer` (`offer_id`,`business_unit_id`,`points_required`) VALUES (23, 12, 60);
INSERT INTO `offer` (`offer_id`,`business_unit_id`,`points_required`) VALUES (24, 13, 100);
INSERT INTO `offer` (`offer_id`,`business_unit_id`,`points_required`) VALUES (25, 14, 30);
INSERT INTO `offer` (`offer_id`,`business_unit_id`,`points_required`) VALUES (26, 14, 150);
INSERT INTO `points_balance` (`points_balance_id`,`user_id`,`business_unit_id`,`points`,`record_created`) VALUES (31, 27, 11, 10, '2013-04-01');
INSERT INTO `points_balance` (`points_balance_id`,`user_id`,`business_unit_id`,`points`,`record_created`) VALUES (32, 27, 11, 30, '2013-04-02');
INSERT INTO `points_balance` (`points_balance_id`,`user_id`,`business_unit_id`,`points`,`record_created`) VALUES (33, 27, 12, 10, '2013-03-02');
INSERT INTO `points_balance` (`points_balance_id`,`user_id`,`business_unit_id`,`points`,`record_created`) VALUES (34, 27, 12, 20, '2013-03-04');
INSERT INTO `points_balance` (`points_balance_id`,`user_id`,`business_unit_id`,`points`,`record_created`) VALUES (34, 27, 14, 20, '2013-04-12');
INSERT INTO `points_balance` (`points_balance_id`,`user_id`,`business_unit_id`,`points`,`record_created`) VALUES (34, 27, 14, 100, '2013-04-14');
积分余额表中的记录是仅插入的,因此用户的单位余额在该用户/单位的最新记录中
同一业务内的业务单位共享积分余额(即用户的总积分是他们在该业务内的各个单位上的积分总和)。
我想选择用户有足够积分兑换的优惠。
SELECT up.user_id, up.points, o.*
FROM offer o
JOIN (
SELECT user_id, business_id, sum(points) AS points
FROM points_balance pb
JOIN business_unit bu on pb.business_unit_id = bu.business_unit_id
WHERE pb.points_balance_id IN (
SELECT MAX(pb2.points_balance_id)
FROM points_balance pb2
WHERE pb2.user_id = 27
GROUP BY pb2.business_unit_id)
GROUP BY user_id, business_id
) up ON up.points >= o.points_required
上面的查询多次返回同一优惠的一个实例(乘以用户拥有的点数):(
我正在寻找的结果集是:
user_id | points | offer_id
27 | 50 | 21
27 | 50 | 22
27 | 100 | 24
27 | 100 | 25
谢谢大家。G