2011-05-20 48 views
0

由於這是一個奇怪的問題,我找不到正確的答案。我正在爲使用codeigniter的公司開發一個內部網。我有一個讓我得到的所有產品在某一天的查詢語法:MySQL在單行中選擇班次

$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,8: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時間戳。

謝謝你的建議。

Best Redgards。

Gokhan

+0

urk。日期爲int11。您爲$ start_date和$ end_date使用了什麼值。我想你可能只需要添加一些時間。 – 2011-05-20 10:12:52

+0

對不起,我忘了提及它。使用unix時間戳。開始日期是00:00,結束日期是24:00。有很多記錄(1年)。 – Valour 2011-05-20 10:17:01

+0

老實說,我要寫一個答案,但我意識到你正在使用代碼點火器調用,而不是標準的MySql。我無法確定我在那裏寫的任何內容會有幫助。 我希望你認識到,只需添加到$ start_date和$ end_dates,並多次調用查詢,即可獲取數據?即,shift2 start_date是$ start_date +(60 * 60 * 8),$ end_date是$ end_date +(60 * 60 * 16) – 2011-05-20 10:24:58

回答

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