2012-03-07 70 views
4

MySQL表的MySQL增加新的列在查詢結果

reject_data

+-----------+-----------------+------------------+---------------+ 
| reject_id | reject_location | reject_equipment | reject_time | 
+-----------+-----------------+------------------+---------------+ 
| 1   | 7    | 6    | 1326795921000 | 
+-----------+-----------------+------------------+---------------+ 
| 2   | 7    | 1    | 1326796641000 | 
+-----------+-----------------+------------------+---------------+ 
| 3   | 7    | 6    | 1326799521000 | 
+-----------+-----------------+------------------+---------------+ 
| 4   | 6    | 5    | 1326800781000 | 
+-----------+-----------------+------------------+---------------+ 
| 5   | 7    | 3    | 1326802281000 | 
+-----------+-----------------+------------------+---------------+ 
| 6   | 7    | 4    | 1326802941000 | 
+-----------+-----------------+------------------+---------------+ 
| 7   | 7    | 1    | 1326814161000 | 
+-----------+-----------------+------------------+---------------+ 
| 8   | 6    | 2    | 1328026700000 | 
+-----------+-----------------+------------------+---------------+ 

設備

+--------------+------------------+ 
| equipment_id | equipment_string | 
+--------------+------------------+ 
| 1   | Microdoser  | 
+--------------+------------------+ 
| 2   | Monoblock  | 
+--------------+------------------+ 
| 3   | Valve Magnet  | 
+--------------+------------------+ 
| 4   | Checkweigher  | 
+--------------+------------------+ 
| 5   | Microleak  | 
+--------------+------------------+ 
| 6   | Capper   | 
+--------------+------------------+ 

位置

+-------------+-----------------+ 
| location_id | location_string | 
+-------------+-----------------+ 
| 1   | Fred Line 1  | 
+-------------+-----------------+ 
| 2   | Fred Line 2  | 
+-------------+-----------------+ 
| 3   | Fred Line 3  | 
+-------------+-----------------+ 
| 4   | Bob Line 1  | 
+-------------+-----------------+ 
| 5   | Bob Line 2  | 
+-------------+-----------------+ 
| 6   | Bob Line 3  | 
+-------------+-----------------+ 
| 7   | Jeff Line 1  | 
+-------------+-----------------+ 
| 8   | Jeff Line 2  | 
+-------------+-----------------+ 
| 9   | Jeff Line 3  | 
+-------------+-----------------+ 

emrs_d ATA

+---------+---------------+-----------+-------------+---------------+ 
| emrs_id | emrs_location | emrs_code | emrs_string | emrs_time  | 
+---------+---------------+-----------+-------------+---------------+ 
| 1  | 8    | 8744751 | String Text | 1331051832000 | 
+---------+---------------+-----------+-------------+---------------+ 
| 2  | 3    | 8660465 | String Text | 1331051832000 | 
+---------+---------------+-----------+-------------+---------------+ 
| 3  | 6    | 8665447 | String Text | 1331055356000 | 
+---------+---------------+-----------+-------------+---------------+ 
| 4  | 7    | 8762177 | String Text | 1331060531000 | 
+---------+---------------+-----------+-------------+---------------+ 
| 5  | 4    | 8547253 | String Text | 1331061898000 | 
+---------+---------------+-----------+-------------+---------------+ 
| 6  | 9    | 8744580 | String Text | 1331062654000 | 
+---------+---------------+-----------+-------------+---------------+ 
| 7  | 2    | 8668716 | String Text | 1331064810000 | 
+---------+---------------+-----------+-------------+---------------+ 
| 8  | 1    | 8665436 | String Text | 1331066757000 | 
+---------+---------------+-----------+-------------+---------------+ 
| 9  | 5    | 8761458 | String Text | 1331066847000 | 
+---------+---------------+-----------+-------------+---------------+ 
| 10  | 8    | 8743520 | String Text | 1331068372000 | 
+---------+---------------+-----------+-------------+---------------+ 
| 11  | 3    | 8708691 | String Text | 1331070587000 | 
+---------+---------------+-----------+-------------+---------------+ 
| 12  | 7    | 8811149 | String Text | 1331071045000 | 
+---------+---------------+-----------+-------------+---------------+ 

當前查詢

下面的查詢是我目前使用的,請注意,我用「IN」,因爲這是動態的,同爲「之間」的值。

SELECT location_string, equipment_string, reject_time 
FROM reject_data, equipment, locations 
WHERE reject_equipment = equipment_id 
    AND reject_location = location_id 
    AND reject_location IN (7) 
    AND reject_equipment IN (1,2,3,4,5,6) 
    AND reject_time BETWEEN 0 AND 1331113803000 
ORDER BY reject_id DESC 
LIMIT 100 

問題

我想這樣做的是增加兩列包含emrs_code和emrs_string的權利,請參閱下表作爲一個例子。我遇到的問題是試圖爲每個拒絕記錄獲取相關的emrs_code和emrs_string,我只想返回一組emrs結果,每個拒絕,並且emrs數據需要從最近的前一個時間與reject_time比較,有點難至所以在這裏解釋是查詢得到我predifined位置和時間正確的信息:

SELECT emrs_code, emrs_string 
FROM `rejectlogging`.`emrs_data` 
WHERE emrs_time <= 1331113803000 AND emrs_location = 7 
ORDER BY emrs_time DESC 
LIMIT 1; 

我基本上停留在獲取上述合併到原始查詢。任何幫助將非常感激。謝謝。

所需的結果

+-----------------+------------------+---------------+-----------+-------------+ 
| location_string | equipment_string | reject_time | emrs_code | emrs_string | 
+-----------------+------------------+---------------+-----------+-------------+ 
| A7    | Microleak  | 1331064910000 | 8762177 | String Text | 
+-----------------+------------------+---------------+-----------+-------------+ 
| A3    | Checkweigher  | 1331107261000 | 8708691 | String Text | 
+-----------------+------------------+---------------+-----------+-------------+ 
| A1    | Microdoser  | 1331107166000 | 8665436 | String Text | 
+-----------------+------------------+---------------+-----------+-------------+ 
| A2    | Microdoser  | 1331107161000 | 8668716 | String Text | 
+-----------------+------------------+---------------+-----------+-------------+ 
| A4    | Microleak  | 1331105836000 | 8547253 | String Text | 
+-----------------+------------------+---------------+-----------+-------------+ 
+0

+1只是放在問題的答覆 – Ben 2012-03-07 10:56:13

回答

2

嘗試這種:-(修訂版)

SELECT location_string, equipment_string, reject_time , (SELECT emrs_code 
FROM `rejectlogging`.`emrs_data` 
WHERE emrs_time <= reject_data.reject_time AND emrs_location = reject_data.reject_location 
ORDER BY emrs_time DESC 
LIMIT 1) as emrs_code, 
(SELECT emrs_string 
FROM `rejectlogging`.`emrs_data` 
WHERE emrs_time <= reject_data.reject_time AND emrs_location = reject_data.reject_location 
ORDER BY emrs_time DESC 
LIMIT 1) as emrs_string 
FROM reject_data, equipment, locations 
WHERE reject_equipment = equipment_id 
    AND reject_location = location_id 
    AND reject_location IN (7) 
    AND reject_equipment IN (1,2,3,4,5,6) 
    AND reject_time BETWEEN 0 AND 1331113803000 
ORDER BY reject_id DESC 
LIMIT 100 
+0

感謝的努力,佈局是正確的,但對於emrs_code和emrs_string的數據需要各自有關拒絕的emrs_location和emrs_time需要是與找到的每個拒絕記錄相同。 – Matthew 2012-03-07 11:07:07

+0

我已經更新了查詢。請再試一次 – 2012-03-07 11:08:43

+0

這似乎正是我以後的樣子!非常感謝你的幫助,真的很感激。 – Matthew 2012-03-07 11:14:24

0

試試這個:

SELECT location_string, equipment_string, reject_time 
FROM reject_data rd 
    INNER JOIN locations l on rd.reject_location = l.location_id 
    INNER JOIN equipment e on rd.reject_equipment = e.equipment_id 
    INNER JOIN 
    (
     SELECT * FROM emrs_data 
     WHERE emrs_time <= 1331113803000 AND emrs_location = 7 
     ORDER BY emrs_time DESC 
     LIMIT 1 
    ) d ON rd.reject_location = d.emrs_location 
WHERE rd.reject_location IN (7) 
AND rd.reject_equipment IN (1,2,3,4,5,6) 
AND rd.reject_time  BETWEEN 0 AND 1331113803000 
ORDER BY rd.reject_id DESC 
LIMIT 100 
+0

感謝,越來越 - 錯誤代碼:1054.未知的列'ed.emrs_location'在'子句' – Matthew 2012-03-07 11:08:42

+0

@Matthew,對不起,它是'd.emrs_location'我修正了,d是子查詢的別名代表你的第二個查詢。 – 2012-03-07 11:10:41

0

假設時間和位置的字符串是使用什麼加入(雖然理論上不可能有多個emrs在同一時間和地點?

SELECT 
    location_string, 
    equipment_string, 
    reject_time, 
    emrs_code, 
    emrs_string 
FROM 
    reject_data 
    INNER JOIN equipment 
     ON (reject_equipment = equipment_id) 
    INNER JOIN locations 
     ON (reject_location = location_id) 
    INNER JOIN emrs_data 
     ON 
     (
      reject_time = emrs_time 
      AND 
      reject_location = emrs_location 
     ) 
WHERE 
    reject_location IN (7) 
    AND 
    reject_equipment IN (1,2,3,4,5,6) 
    AND 
    reject_time BETWEEN 0 AND 1331113803000 
ORDER BY 
    reject_id DESC 
LIMIT 100