多个mysql语句将数据放入同一个表中

时间:2014-11-24 15:38:48

标签: mysql

我有一个看起来像这样的sql语句。

SELECT * FROM ( 
SELECT Op_Number, Machine_Number, Format(SUM(Elapsed),2) AS Elapsed , SUM(Count) AS Count , SUM(Scrap) AS Scrap , (SUM(`Scrap`)/SUM(`Count`)) * 100 AS ScrapPercent 
FROM count_down 
WHERE Wo_Number != 'PlaceHolder' AND Count > 0
Group BY Op_Number , Machine_Number
 LIMIT 5
) AS tmp_table ORDER BY Machine_Number, Count desc;

此查询返回如下结果:

OP_number | Machine_Number | Elapsed | Count | Scrap | ScrapPerent 
 --------------------------------------------------------------------
  Value1  |     1          |   20     |  10  |  10   |   100
  Value2  |     10         |   30     |  5   |   0   |    0

然而,当我为每台机器提供大量行时,例如,如果机器1有200条记录,机器10有200条,那么我在这个结果集中将有400条记录,所以我想要做的就是将此语句限制为5和ad a WHERE所以,其中Machine_number = 1,

所以最后我想要的是一个像这样工作的查询

//Query for machine1
SELECT * FROM ( 
SELECT Op_Number, Machine_Number, Format(SUM(Elapsed),2) AS Elapsed , SUM(Count) AS Count , SUM(Scrap) AS Scrap , (SUM(`Scrap`)/SUM(`Count`)) * 100 AS ScrapPercent 
FROM count_down 
WHERE Wo_Number != 'PlaceHolder' AND Count > 0 AND Machine_number = 1
Group BY Op_Number , Machine_Number
 LIMIT 5
) AS tmp_table ORDER BY Machine_Number, Count desc;

// Query for machine 2 
  SELECT * FROM ( 
 SELECT Op_Number, Machine_Number, Format(SUM(Elapsed),2) AS Elapsed , SUM(Count) AS Count , SUM(Scrap) AS Scrap , (SUM(`Scrap`)/SUM(`Count`)) * 100 AS ScrapPercent 
FROM count_down 
WHERE Wo_Number != 'PlaceHolder' AND Count > 0 AND Machine_number = 2
Group BY Op_Number , Machine_Number
 LIMIT 5
) AS tmp_table ORDER BY Machine_Number, Count desc;

Ext ext fyi全套将有14台机器,然后我希望结果看起来像这样

    OP_number | Machine_Number | Elapsed | Count | Scrap | ScrapPerent 
 --------------------------------------------------------------------
  Value1  |     1          |   20     |  10  |  10   |   100
  Value1  |     1          |   20     |  10  |  10   |   100
  Value1  |     1          |   20     |  10  |  10   |   100
  Value1  |     1          |   20     |  10  |  10   |   100
  Value1  |     1          |   20     |  10  |  10   |   100
  Value1  |     2          |   20     |  10  |  10   |   100
  Value1  |     2          |   20     |  10  |  10   |   100
  Value1  |     2          |   20     |  10  |  10   |   100
  Value1  |     2          |   20     |  10  |  10   |   100
  Value1  |     2          |   20     |  10  |  10   |   100
  Value1  |     3          |   20     |  10  |  10   |   100
  Value1  |     3          |   20     |  10  |  10   |   100
  Value1  |     3          |   20     |  10  |  10   |   100
  Value1  |     3          |   20     |  10  |  10   |   100
  Value1  |     3          |   20     |  10  |  10   |   100

这是我迄今为止所拥有的一个方案

http://sqlfiddle.com/#!2/d737e2/1

因此,你可以从这个小提琴中看到,我得到了机器10的结果,并将其限制为5个结果,因为只获得前5名,我想要做的是这个查询但是对于14台机器如此绝对的机器1

Where Machine_number = 1;
Where Machine_number = 2;  
Where Machine_number = 3;
ext ext 

然后返回如上所示的结果,但该机器的每个查询都需要限制为5个结果

0 个答案:

没有答案