通过分组的Mysql顺序

时间:2014-02-11 16:39:29

标签: mysql sql

大家好,今天我遇到了问题..

首先,我有一个两个表,每个表我有“product_seq_id”列,我使用相同的“product_seq_id”加入表 在第二个表中,“product_seq_id”有多行,我只想要一个具有以下条件的行

  • table2.date_start not null
  • table2.date_start等于'0000-00-00'或table2.date_start< = CURDATE()
  • table2.date_end等于'0000-00-00'或table2.date_start> = CURDATE()
  • 如果同一天有2个或更多行匹配,则获得最高的table2.priority

我已经做了一些工作..但问题在于,在订购带有分组的列时,它没有取最高优先级的数字

//我的查询

SELECT 
    psp . *, pcp . *
FROM
    sk_product_category_path pcp
        left join
    sk_product_special_price psp ON (psp.product_seq_id = pcp.product_seq_id)
where
    pcp.category_seq_id = 146
        AND psp.product_seq_id IS NOT NULL
        AND CASE
        WHEN
            psp.date_start IS NOT NULL
        THEN
            (psp.date_start = '0000-00-00'
                OR psp.date_start <= CURDATE())
                AND (psp.date_end = '0000-00-00'
                OR psp.date_end >= CURDATE())
        ELSE 1 = 1
    END
group by psp.product_seq_id
order by psp.priority desc

Result Came for above code:
# product_special_price_seq_id, product_special_price, date_start, date_end, priority, product_seq_id, product_category_path_seq_id, product_seq_id, category_seq_id
2309    123123  0000-00-00  0000-00-00  0   3196    1   3196    146
2307    12313   0000-00-00  0000-00-00  0   3197    3   3197    146

Result I wanted:
# product_special_price_seq_id, product_special_price, date_start, date_end, priority, product_seq_id, product_category_path_seq_id, product_seq_id, category_seq_id
2309    12200   0000-00-00  0000-00-00  1   3196    2   3196    146
2307    12313   0000-00-00  0000-00-00  0   3197    3   3197    146

//表数据

CREATE TABLE IF NOT EXISTS `sk_product_category_path` (
  `product_category_path_seq_id` int(11) NOT NULL AUTO_INCREMENT,
  `product_seq_id` int(11) NOT NULL,
  `category_seq_id` int(11) NOT NULL,
  PRIMARY KEY (`product_category_path_seq_id`),
  UNIQUE KEY `product_seq_id` (`product_seq_id`,`category_seq_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

INSERT INTO `sk_product_category_path` (`product_category_path_seq_id`, `product_seq_id`, `category_seq_id`) VALUES
(1, 3196, 146),
(2, 3197, 146),
(3, 3198, 146);

CREATE TABLE IF NOT EXISTS `sk_product_special_price` (
  `product_special_price_seq_id` int(11) NOT NULL AUTO_INCREMENT,
  `product_special_price` bigint(20) DEFAULT NULL,
  `date_start` date DEFAULT NULL,
  `date_end` date DEFAULT NULL,
  `priority` int(11) DEFAULT NULL,
  `product_seq_id` int(11) NOT NULL,
  PRIMARY KEY (`product_special_price_seq_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

INSERT INTO `sk_product_special_price` (`product_special_price_seq_id`, `product_special_price`, `date_start`, `date_end`, `priority`, `product_seq_id`) VALUES
(1, 12313, '0000-00-00', '0000-00-00', 0, 3197),
(2, 12200, '2014-02-11', '2014-02-11', 1, 3197),
(3, 123123, '0000-00-00', '0000-00-00', 0, 3196);

2 个答案:

答案 0 :(得分:0)

在MySQL的GROUP BY期间,它会为每个组选择第一个匹配的行,除非您使用的是聚合函数。第一个匹配不必总是以min(id)为单位。

可能的查询应该是:

SELECT t.*
from table_name t 
inner join (
  select min(id) as id 
  from table_name t 
  group by col) as s
on s.id = t.id

答案 1 :(得分:0)

请查看以下查询..请告诉我这是您的要求吗?

SELECT * FROM sk_product_special_price pspo WHERE pspo.priority IN(SELECT MAX(psp.priority) FROM sk_product_special_price psp JOIN sk_product_category_path pcp ON(pcp.product_seq_id=psp.product_seq_id) WHERE psp.date_start IS NOT NULL AND psp.date_start BETWEEN '0000-00-00' AND CURDATE() AND (psp.date_end>=CURDATE() OR psp.date_end='0000-00-00') AND pcp.product_seq_id=pspo.product_seq_id);

我已将结束日期“'2014-02-11”更新为“2014-02-12”,以便我的代码获取结束日期&gt; =今天的日期。

此查询将返回table2详细信息,即基于priyority值的每个产品的表sk_product_special_price。

输出将是

product_special_price_seq_id, product_special_price, date_start, date_end, priority, product_seq_id 2, 12200, '2014-02-11', '2014-02-12', 1, 3197 3, 123123, '0000-00-00', '0000-00-00', 0, 3196