2012-03-07 72 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