下面有两个表  
表A
id   name
1     a
2     b
3     c
4     d
5     e
表B
type   aid
1       1
2       1
3       2
4       1
5       2
6       3
我想利用这两个表检索所有的东西sql语句如下
select a.*,b.type from A as a left join B as b on a.id = b.aid
但得到的结果A中的id会出现多个  我想只让他取其中一个  应该怎么实现? distinct我试过 好像不行 求高人指点! 

解决方案 »

  1.   

    select a.*,b.type from A as a left join B as b on a.id = b.aid  group by a.id;
      

  2.   


    能告诉我原理吗? 为什么用group by
      

  3.   

    group by 这种语法是被MYSQL支持的。在这种情况下,对所有非GROUP BY中的字段,MYSQL会“任意”选择一行,一般是第一行。
      

  4.   

    MySQL官方文档 http://dev.mysql.com/doc/refman/5.1/zh/index.html·         MySQL对GROUP BY的使用进行了扩展,允许选择在GROUP BY子句中没有被提到的字段。如果您没有得到预期的结果,请阅读GROUP BY的说明,请参见12.10节,“与GROUP BY子句同时使用的函数和修改程序”。
    12.10.3. 具有隐含字段的GROUP BY
    MySQL 扩展了 GROUP BY的用途,因此你可以使用SELECT 列表中不出现在GROUP BY语句中的列或运算。这代表 “对该组的任何可能值 ”。你可以通过避免排序和对不必要项分组的办法得到它更好的性能。例如,在下列问询中,你无须对customer.name 进行分组: mysql> SELECT order.custid, customer.name, MAX(payments)    ->        FROM order,customer    ->        WHERE order.custid = customer.custid    ->        GROUP BY order.custid;在标准SQL中, 你必须将 customer.name添加到 GROUP BY子句中。在MySQL中, 假如你不在ANSI模式中运行,则这个名字就是多余的。 假如你从 GROUP BY 部分省略的列在该组中不是唯一的,那么不要使用这个功能! 你会得到非预测性结果。在有些情况下,你可以使用MIN()和MAX() 获取一个特殊的列值,即使他不是唯一的。下面给出了来自包含排序列中最小值的列中的值: SUBSTR(MIN(CONCAT(RPAD(sort,6,' '),column)),7)See 3.6.4节,“拥有某个字段的组间最大值的行”. 注意,假如你正在尝试遵循标准 SQL, 你不能使用GROUP BY或 ORDER BY子句中的表达式。你可以通过使用表达式的别名绕过这一限制:  mysql> SELECT id,FLOOR(value/100) AS val     -> FROM tbl_name    -> GROUP BY id, val ORDER BY val;然而, MySQL允许你使用GROUP BY 及 ORDER BY 子句中的表达式。例如:mysql> SELECT id, FLOOR(value/100) FROM tbl_name ORDER BY RAND();
      

  5.   

    你想要哪个ID就在后面加上条件啊,select a.*,b.type from A as a left join B as b on a.id = b.aid where a.id=(你想要的ID)
      

  6.   

    [code=SQL]if object_id('tempdb.dbo.#TA') is not null drop table #TA
    go 
    create table #TA(id varchar(10),names varchar(10))
    insert #TA
    select    '1' ,   'a'  union all
    select    '2' ,   'b'  union all
    select    '3' ,   'c'  union all
    select    '4'  ,  'd'  union all
    select    '5'  ,  'e'   if object_id('tempdb.dbo.#TB') is not null drop table #TB
    go 
    create table #TB(types varchar(10),aid varchar(10))
    insert #TB
    select    '1' ,   '1'  union all
    select    '2' ,   '1'  union all
    select    '3' ,   '2'  union all
    select    '4'  ,  '1'  union all
    select    '5'  ,  '2'  union all
    select    '6'  ,  '3'   
    select * from #TA  right join #TB  on #TA.id = #TB.aid [/code  ]
    id    names   types   aid 
    1 a 1 1
    1 a 2 1
    2 b 3 2
    1 a 4 1
    2 b 5 2
    3 c 6 3
      

  7.   

    if object_id('tempdb.dbo.#TA') is not null drop table #TA
    go 
    create table #TA(id varchar(10),names varchar(10))
    insert #TA
    select    '1' ,   'a'  union all
    select    '2' ,   'b'  union all
    select    '3' ,   'c'  union all
    select    '4'  ,  'd'  union all
    select    '5'  ,  'e'   if object_id('tempdb.dbo.#TB') is not null drop table #TB
    go 
    create table #TB(types varchar(10),aid varchar(10))
    insert #TB
    select    '1' ,   '1'  union all
    select    '2' ,   '1'  union all
    select    '3' ,   '2'  union all
    select    '4'  ,  '1'  union all
    select    '5'  ,  '2'  union all
    select    '6'  ,  '3'   
    select * from #TA  right join #TB  on #TA.id = #TB.aid 
    ---------运行结果-----------
    id    names  types  aid 
    1 a 1 1
    1 a 2 1
    2 b 3 2
    1 a 4 1
    2 b 5 2
    3 c 6 3