SQL语句加入三个表

时间:2014-02-26 05:57:33

标签: mysql sql innodb tokudb

我有以下声明

SELECT di_id, 
       di_name, 
       di_location, 
       ig_name, 
       in_latitude, 
       in_longitude
FROM dam_info
LEFT JOIN instrument_group 
       ON ig_diid = di_id
LEFT JOIN instruments 
       ON in_igid = ig_id;

返回结果如下

di_id  di_name        di_location           ig_name                 in_latitude   in_longitude
13     Macap          "Kluang, Johor"       "Standpipe Piezometer"  1.890895      103.266853
13     Macap          "Kluang, Johor"       "Standpipe Piezometer"  1.888353      103.267067
1      "Timah Tasoh"  "Kangar, Perlis"      NULL                    NULL          NULL
2      "Padang Saga"  "Langkawi, Kedah"     NULL                    NULL          NULL
3      "Bukit Kwong"  "Pasir Mas, Kelantan" NULL                    NULL          NULL
4      "Bukit Merah"  "Kerian, Perak"       NULL                    NULL          NULL
5      Gopeng         "Gopeng, Perak"       NULL                    NULL          NULL
6      Repas          "Bentong, Pahang"     NULL                    NULL          NULL
7      Batu           "Gombak, Selangor"    NULL                    NULL          NULL
8      Pontian        "Rompin, Pahang"      NULL                    NULL          NULL
9      "Anak Endau"   "Rompin, Pahang"      NULL                    NULL          NULL
10     Labong         "Mersing, Johor"      NULL                    NULL          NULL
11     Bekok          "Batu Pahat, Johor"   NULL                    NULL          NULL
12     Sembrong       "Batu Pahat, Johor"   NULL                    NULL          NULL
14     Perting        "Bentong, Pahang"     NULL                    NULL          NULL
15     Beris          "Sik, Kedah"          NULL                    NULL          NULL

从结果中可以看出,有一个重复的行,我想消除其中一个,如果table乐器有更多的行要返回,那么我只想要一个。

实现这一目标的正确声明是什么?

提前致谢

3 个答案:

答案 0 :(得分:2)

你也可以使用group by子句:

SELECT di_id, 
       di_name, 
       di_location, 
       ig_name, 
       in_latitude, 
       in_longitude 
FROM   dam_info 
       LEFT JOIN instrument_group 
              ON ig_diid = di_id 
       LEFT JOIN instruments 
              ON in_igid = ig_id 
GROUP  BY di_id; 

答案 1 :(得分:1)

尝试在select关键字后添加关键字DISTINCT。

SELECT DISTINCT di_id, 
                di_name, 
                di_location, 
                ig_name, 
                in_latitude, 
                in_longitude 
FROM   dam_info 
       LEFT JOIN instrument_group 
              ON ig_diid = di_id 
       LEFT JOIN instruments 
              ON in_igid = ig_id; 

答案 2 :(得分:1)

在您的架构中,我认为有一个di_id包含多个工具,即instruments表包含多个in_igid的多个记录,作为短语'instrument_group '建议。

你可以做的一件有意义的事情就是返回乐器的边界框:

SELECT di_id, di_name, di_location, ig_name,
    MIN(in_latitude) as min_latitude,
    MAX(in_latitude) as max_latitude,
    MIN(in_longitude) as min_longitude,
    MAX(in_longitude) as max_longitude
FROM dam_info
left join instrument_group on ig_diid = di_id
left join instruments on in_igid = ig_id
GROUP BY di_id, di_name, di_location, ig_name;