2013-05-18 104 views
0

我寫了下面的SQL語句,在測試表,我有4個記錄,並對coa_body表我也有四個記錄,這個查詢:MySQL來返回獨特的記錄

SELECT `name`, `compedia`, `specification` 
    FROM (`tests` t, `coa_body` cb) 
    JOIN 
     `request_details` rd 
    ON `t`.`id` = `rd`.`test_id` 
WHERE `rd`.`request_id` = 'NDQA201303001' AND cb.labref = 'NDQA201303001'; 

回報:

1.name compedia說明書

Assay      Alphy  poxy 
pH(Acidity/Alkalinity) Alphy  poxy 
Sterility     Alphy  poxy 
Bacterial Endotoxin Test Alphy  poxy 
Assay      poxy  alphy 
pH(Acidity/Alkalinity) poxy  alphy 
Sterility     poxy  alphy 
Bacterial Endotoxin Test poxy  alphy 
Assay      alphy1 poxy1 
pH(Acidity/Alkalinity) alphy1 poxy1 
Sterility     alphy1 poxy1 
Bacterial Endotoxin Test alphy1 poxy1 
Assay      poxy1  alphy1 
pH(Acidity/Alkalinity) poxy1  alphy1 
Sterility     poxy1  alphy1 
Bacterial Endotoxin Test poxy1  alphy1 

2.coa_body表如下: ID labref compedia規範

1 NDQA201303001 Alphy poxy 
2 NDQA201303001 poxy alphy 
3 NDQA201303001 alphy1 poxy1 
4 NDQA201303001 poxy1 alphy1 

3.我只需要4條一模一樣這樣

name      compedia specification 

    Assay      Alphy poxy 
    pH(Acidity/Alkalinity) poxy alphy 
    Sterility     alphy1 poxy1 
    Bacterial Endotoxin Test poxy1 alphy1 

4.at查詢結束的coa_body表,如果我添加GROUP BY t.name語句,它返回

name     compedia specification  

Assay      Alphy poxy 
Bacterial Endotoxin Test Alphy poxy 
pH(Acidity/Alkalinity)  Alphy poxy 
Sterility     Alphy poxy 

這是不是想讓我想要的,我想要的結果是一個在3號

+2

請不要使用不適用於您的問題的標籤 –

+0

好的,下次再記住這一點 – alphy

回答

-1

重寫查詢

SELECT `name`, `compedia`, `specification` 
    FROM (`tests` t, `coa_body` cb) 
    JOIN 
     `request_details` rd 
    ON `t`.`id` = `rd`.`test_id` 
WHERE `rd`.`request_id` = 'NDQA201303001' AND cb.labref = 'NDQA201303001' GROUP BY name;