Sql查询 - 无法选择行的最大值

时间:2017-07-09 04:48:23

标签: mysql

我有这个查询,显示我的网站中的项目。

    $sql = "
select p.id
     , p.name
     , p.logo
     , p.short_description
     , c.Parent_category
     , IF(d.commission_name = 'percent'
          , CONCAT(FORMAT(d.action, 2)/2 ,' %') 
          , CONCAT(FORMAT(d.action,2)/2,' €')) as comms
     , GROUP_CONCAT(c.category_name SEPARATOR ',') as category_names  
  from eshop p  
  join eshop_cat c 
    on p.id = c.eshop_id 
  join eshop_commissions d 
    on p.id = d.eshop_id 
 where c.Parent_category = 'fashion'
   and p.sites = 1  
 GROUP 
    BY d.action DESC 
";

问题出在这里

IF(d.commission_name = 'percent', CONCAT(FORMAT(d.action, 2)/2 ,' %') , 
    CONCAT(FORMAT(d.action,2)/2,' €'))

我只需要每个eshop id的最大价值。我试过这个

IF(d.commission_name = 'percent', CONCAT(FORMAT(max(d.action), 2)/2 ,' %') , 
CONCAT(FORMAT(max(d.action),2)/2,' €'))

但它不起作用

例如,我有2个具有相同eshop id的条目 我怎样才能收到最多的1个佣金?

eshop_id commission_name action
       1 percent             20 (%)
       1 fixed_amount        30 (euro)

EDITED

CREATE TABLE `eshop` (
  `id` int(11) NOT NULL,
  `name` text NOT NULL,
  `logo` text NOT NULL,
  `short_description` text NOT NULL,
  `sites` tinyint(1) NOT NULL,
  `url` text NOT NULL,
  `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `summary` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `eshop_commissions` (
  `commission_name` text NOT NULL,
  `eshop_id` int(11) NOT NULL,
  `action` decimal(10,2) NOT NULL,
  `subaction` decimal(10,2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `eshop_cat` (
  `eshop_id` int(11) NOT NULL,
  `category_name` text NOT NULL,
  `Parent_category` text
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `eshop_commissions` (`commission_name`, `eshop_id`, `action`, `subaction`) VALUES
('percent', 100, '12.00', '12.00'),
('fixed_amount', 100, '15.00', '15.00'),
('percent', 100, '5.00', '5.00'),
('percent', 100, '2.00', '2.00');



INSERT INTO `eshop` (`id`, `name`, `logo`, `short_description`, `description`, `sites`, `url, `timestamp`, `summary`) VALUES
(100, 'Tokotoukan', 'logo-tokotoukan.gif', '<p>\r\n !</p>\r\n', '<p>\r\n    <font face=\"arial\" size=\"2\">Τo TOKOTOUKAN </font></p>\r\n', 1, 'http://www.com', ''2017-07-07 17:39:59', '15,00%');

INSERT INTO `eshop_cat` (`eshop_id`, `category_name`, `Parent_category`) VALUES
(100, 'fashion', 'fashion');

0 个答案:

没有答案