开发者

MySQL selecting shifts in a single row

Since it is a weird question, i couldn't find the right answer. I am developing an intranet for my company using codeigniter. I have a query syntax that lets me get all products in a given day:

$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();

and here it is syntax as 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

I am calling this query for just once.

I am getting result like this:

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

And it works perfectly well. But I need to get the production between work shifts. there is tree work shifts: 00:00 - 08:00, 08:00 - 16:00, 16:00 - 24:00. How can I get work shifts for each product?

I mean I need to get result like this:

| 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            |
+----------+----------+----------+---------+---------+---------+----------------+

And my Tables like this:

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,
  `c开发者_如何学Pythonode_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 ;

So. Any idea?

Edit: I forget to mention it. I am using Unix timestamp.

Thank you for advice.

Best Redgards.

Gokhan


I'm tired and shouldn't be writing code anymore. I'd think it would look like this though.

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜