0

我已经与这个查询作斗争了一天多。我的 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

4

2 回答 2

0

这是一个执行我认为您想要的查询的查询:

select up.user_id, up.business_id, up.allpoints, o.*
from (select user_id, business_id, sum(points_balance) as allpoints
      from points_balance pb join
           business_Unit bu
           on pb.business_unit_id = bu.business_unit_id
      group by user_id, business_id
     ) up join
     offers o
     on o.point_required <= up.allpoints;

此查询背后的逻辑比您采用的方法简单得多。第一个子查询计算企业内所有业务部门中用户可用的总点数。然后它找到匹配点。

where您可以在子查询或整体查询中添加子句,以限制特定的企业或用户。

此外,这是基于问题文本中的字段名称,而不是查询中的字段名称。它们是不同的。

于 2013-05-19T14:01:17.640 回答
0

当我有积分在多个业务中兑换优惠时,我发现它返回了同一优惠的多个实例。所以问题是我没有将报价与特定业务联系起来!所以我添加到查询的底部

JOIN business_unit bu ON bu.business_unit_id = o.business_unit_id
AND up.business_id = bu.business_id

排序:)

于 2013-05-20T23:16:23.377 回答