2012-03-01 75 views
0

我有以下表格:MySQL我如何只顯示以下查詢的最大值/最大值?

mysql> select * from F_FINDINGS; 
+---------+------------+ 
| F_ID | CONFIDENCE | 
+---------+------------+ 
|  1 |  100 | 
|  2 |   70 | 
|  2 |  0.35 | 
|  1 |  100 | 
+---------+------------+ 

mysql> select * from F_THINGS; 
+---------+-------------------+--------+------------+ 
| F_ID | F_TITLE  | S_ID | F_VALUE | 
+---------+-------------------+--------+------------+ 
|  1 | STUFF A   |  1 |  1.1 |  
|  2 | STUFF C   |  1 |  202.2 | 
|  3 | OBJECT P   |  1 |  10.7 |   
|  4 | Things X   |  2 |  540 | 
|  5 | STUFF D   |  2 |  1080 | 
|  6 | OBJECT M   |  2 |  455 |  
|  7 | Things A   |  3 |  333 |    
|  8 | Things B   |  4 |  825 |   
|  9 | STUFF A   |  1 |  103.4 |   
|  10 | STUFF A   |  1 |  98.4 | 
+---------+-------------------+--------+------------+ 

    mysql> select * from DUMP; 
    +--------+----------+ 
    | D_ID | D_NAME | 
    +--------+----------+ 
    |  1 | E0  | 
    |  2 | A1  | 
    |  3 | AB  | 
    +--------+----------+ 

mysql> select * FROM STUFF; 
+--------+--------+-------------------+ 
| S_ID | D_ID | S_TITLE   | 
+--------+--------+-------------------+ 
|  1 |  1 | plastic   | 
|  2 |  2 | metal    | 
|  3 |  3 | wood    | 
|  4 |  3 | gel    | 
+--------+--------+-------------------+ 

我使用下面的查詢

SELECT 
d.D_NAME, 
s.S_TITLE, 
f.F_VALUE, 
IF(r.CONFIDENCE IS NULL, 'N/A', CONCAT((r.CONFIDENCE),'%')AS CONFIDENCE 

FROM F_THINGS f 

JOIN STUFF s ON s.S_ID=f.S_ID 
JOIN DUMP d on d.D_ID=s.D_ID 

LEFT JOIN F_FINDINGS r ON f.F_ID=r.F_ID; 

所需的結果:

+----------+-----------+------------+------------+ 
| D_NAME | S_TITLE | F_VALUE | CONFIDENCE | 
+----------+-----------+------------+------------+ 
| E0  | plastic |  202.2 | 70%  | 
| E0  | plastic |  10.7 | N/A  | 
| E0  | plastic |  1.1 | 100%  | 
| A1  | metal |  540 | N/A  | 
| A1  | metal |  1080 | N/A  | 
| A1  | metal |  455 | N/A  | 
| AB  | wood  |  333 | N/A  | 
| AB  | wood  |  825 | N/A  | 
| E0  | gel  |  103.4 | N/A  | 
| E0  | gel  |  98.4 | N/A  | 
+----------+-----------+------------+------------+ 

我只想要顯示的最高值每個

+0

你的「STUFF」樣本數據在哪裏......還有,最高值?或最高的信心?只要確保 – DRapp 2012-03-01 16:35:31

+0

@DRapp我會添加'東西'表。對不起,我把它留下了。我想要最高的信心 – stackoverflow 2012-03-01 16:37:32

+0

@VAShhh期望的輸出被列在最下面,它表示'期望的結果' – stackoverflow 2012-03-01 16:41:17

回答

1

這應該這樣做,但是在STUFF中沒有值給你想要的結果('凝膠'和'塑料'將需要相同的S_ID和D_ID,並仍然有所不同),這使得結果與你所期望的結果稍有不同。您可能要添加STUFF下一次,所以沒有人需要逆向它;-)

SELECT 
d.D_NAME, 
s.S_TITLE, 
f.F_VALUE, 
IF(MAX(r.CONFIDENCE) IS NULL, 'N/A', CONCAT((MAX(r.CONFIDENCE)),'%')) AS CONFIDENCE 
FROM F_THINGS f 
JOIN STUFF s ON s.S_ID=f.S_ID 
JOIN DUMP d on d.D_ID=s.D_ID 
LEFT JOIN F_FINDINGS r ON f.F_ID=r.F_ID 
GROUP BY D_NAME, S_TITLE, F_VALUE; 

演示here

還添加了一個演示,其中修正了F_THINGS中的值(對於想要與凝膠和塑料分組的行的不同S_ID),從而使您需要的分組成爲here

+0

感謝約阿希姆Isaksson – stackoverflow 2012-03-01 17:31:58

+0

我可能不完全理解「group by」的一個基本方面,但爲什麼它只顯示最高? (畢竟是我之後的) – stackoverflow 2012-03-01 17:37:53

+1

@ Gah_Jamn-it一組由具有相同D_NAME,S_TITLE和F_VALUE的行組成。最高置信度將顯示每組最高。 – 2012-03-01 18:09:46

1

嘗試...

ORDER BY信心DESC

作爲查詢的最後一行

+0

我只想顯示每個值的最高值。排序方式將顯示全部 – stackoverflow 2012-03-01 16:25:56

+0

如何:'訂單放心DESC LIMIT 1' – Travesty3 2012-03-01 16:57:07

+0

也許這與一個group by子句然後 – user466764 2012-03-02 08:37:34

0

然後,只需改變你的

LEFT JOIN F_FINDINGS r ON f.F_ID=r.F_ID; 

LEFT JOIN (select F_ID, MAX( CONFIDENCE) as Confidence 
       from F_Findings 
       group by F_ID) as r 
    on F.F_ID = r.F_ID