0

我写了以下 sql 语句,在 test 表上,我有 4 条记录,在 coa_body 表上我也有 4 条记录,这个查询:

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.名称百科规范

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 specification

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

3.我只需要4条和coa_body表完全一样的记录就这样

   name                     compedia   specification

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

4.在查询结束时如果我添加 GROUP BY t.name 语句,它会返回

 name                   compedia  specification     

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

这不是我想要的,我想要的结果是第 3 位的结果

4

1 回答 1

-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;
于 2013-05-18T13:10:25.383 回答