MySQL选择单行转换

时间:2011-05-20 10:10:24

标签: mysql syntax

由于这是一个奇怪的问题,我找不到正确的答案。我正在使用codeigniter为我的公司开发内部网。我有一个查询语法,可以让我在一天内获得所有产品:

$query = $this->db
         ->select('
             stock_meta.code_company AS product,
             stock_meta.company AS company,
             stock_meta.factory AS factory,
             COUNT(production.product) AS total_product
             ')
         ->from('production')
         ->join('stock_meta', 'production.product = stock_meta.code_local', 'inner')
         ->where('date BETWEEN ' . $start_date . ' AND ' . $end_date)
         ->group_by('product')
         ->order_by('factory')
         ->get();

这里的语法是MySQL:

SELECT
    stock_meta.code_company AS product,
    stock_meta.company AS company,
    stock_meta.factory AS total_product
FROM
    production
INNER JOIN
    stock_meta ON production.product = stock_meta.code_local
WHERE
    date BETWEEN 1304208650 AND 1304280234
GROUP BY
    product
ORDER BY
    factory

我只调用此查询一次。

我得到的结果如下:

| product  | company  | factory  | total_product  |
+----------+----------+----------+----------------+
| 231234   | A        | Fac1     | 475            |
| 245214   | A        | Fac2     | 246            |
+----------+----------+----------+----------------+

它的效果非常好。但我需要在工作班次之间进行制作。树木工作班次:00:00 - 08:00,08:00-16:00,16:00-24:00。如何为每种产品换班?

我的意思是我需要得到这样的结果:

| product  | company  | factory  | shift1  | shift2  | shift3  | total_product  |
+----------+----------+----------+---------+---------+---------+----------------+
| 231234   | A        | Fac1     | 100     | 200     | 175     | 475            |
| 245214   | A        | Fac2     | 46      | 50      | 150     | 246            |
| 500231   | B        | aFaca1   | 46      | 50      | 150     | 246            |
+----------+----------+----------+---------+---------+---------+----------------+

我的桌子是这样的:

CREATE TABLE IF NOT EXISTS `production` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `factory` varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL,
  `date` int(11) DEFAULT NULL,
  `operator` int(11) DEFAULT NULL,
  `product` varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ;

CREATE TABLE IF NOT EXISTS `stock_meta` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `code_local` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
  `code_company` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
  `company` varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL,
  `factory` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
  `status` tinyint(4) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=242 ;

因此。有什么想法吗?

编辑:我忘了提及它。我正在使用Unix时间戳。

感谢您的建议。

最佳红人队。

格克

1 个答案:

答案 0 :(得分:0)

我很累,不应再写代码了。我认为它看起来会像这样。

SELECT
   stock_meta.code_company AS product,
   stock_meta.company AS company,
   stock_meta.factory AS total_product,
   s1.shift1,
   s2.shift2,
   s2.shift3,
   stock_meta.factory AS total_product
FROM
   production
INNER JOIN
   stock_meta ON production.product = stock_meta.code_local
LEFT JOIN 
   ( SELECT stock_meta.code_company AS product, 
        COUNT(production.product) AS shift1
     FROM production, stock_meta
     WHERE production.product = stock_meta.code_local
     AND date BETWEEN '.$start_date.' AND '.strtotime('+8 hour', $start_date).'
   ) as s1 USING (product)
LEFT JOIN 
   ( SELECT stock_meta.code_company AS product, 
        COUNT(production.product) AS shift2
     FROM production, stock_meta
     WHERE production.product = stock_meta.code_local
     AND date BETWEEN '.strtotime('+8 hour', $start_date).' AND '.strtotime('+16 hour', $start_date).'
   ) as s3 USING (product)
LEFT JOIN 
   ( SELECT stock_meta.code_company AS product, 
       COUNT(production.product) AS shift3
     FROM production, stock_meta
     WHERE production.product = stock_meta.code_local
     AND date BETWEEN '.strtotime('+16 hour', $start_date).' AND '.strtotime('+24 hour', $start_date).'
   ) as s3 USING (product)
WHERE
   date BETWEEN '.$start_date.' AND '.strtotime('+16 hour', $start_date).'
GROUP BY
   product
ORDER BY
   total_product