本帖最后由 j_now 于 2009-12-19 14:18:12 编辑

解决方案 »

  1.   

    假设表T id, col
    1   1
    1   2你手工先算一下select 
    from t a left join t on a.id=b.id and a.col<b.col   
    然后写出你的解释,这样别人就可以根据你目前了解的水平帮你进一步解释了。
      

  2.   

    应该是 
    select *
    from t a left join t b on a.id=b.id and a.col <b.col   吧
    少了个 b?什么也不会返回?
      

  3.   

    id 不是主键..
    id, col
    1  1
    1  2select *
    from t a left join t b on a.id=b.id and a.col <b.col1 1 1 2只有这一行因为  a.id=b.id ,而后 a.col < b.col  的就这一个
      

  4.   

    我知道你哪儿不理解了select *
    from t a left join t b on a.id=b.id and a.col  <b.colselect *
    from t a inner join t b on a.id=b.id and a.col  <b.col那inner join 和 left join 的差别在哪儿?
      

  5.   

    你需要理解一下 inner join 和 left join 的差别。
      

  6.   

    内连接 就是有条件的笛卡尔积,并且条件必须满足,说白了就是交集
    外连接 也是交集,不过如果没有相交上的,也显示出来,例如左外:
      a left join b on a.id = b.id
    如果 a.id=3,not exists(b.id=3),那么也把 a.id=3 的记录加入到结果集中
    如果结果集中包括 b.*,那么b.* 使用 null 作为返回值内连接 和外连接 都是交集,只是返回结果稍微不同我主要不能理解的是
    drop table if exists shop;
    create table shop (article varchar(4), dealer varchar(4),
                    price decimal(10,4)) engine='memory';
    insert into shop values ('0001', 'A', 3.99);
    insert into shop values ('0001', 'B', 10.99);
    insert into shop values ('0001', 'C', 1.69);
    insert into shop values ('0001', 'D', 19.95);
    # 这个语句居然能够找到所求:
    SELECT s1.article, s1.dealer, s1.price
    FROM shop s1
    LEFT JOIN shop s2 ON s1.article = s2.article AND s1.price < s2.price
    WHERE s2.article IS NULL;
    1. s1.price < s2.price ,不是要查询最大的吗,这不是查最小的吗
    2. where s2.article IS NULL; 怎么能让 s2.article 是 null 呢?我不知道的是 join 中指定的条件先被执行 还是 where 中的条件先被执行 ...
    您能文字描述一下上面的 sql 语句执行过程吗,辛苦、谢谢,我出去一趟 ....
      

  7.   


    假设表 A, BA               B
    id  colA        id  colB
    1   a1          1   b1
    2   a2          3   b3请你列出
    select * from a inner join b on a.id=b.id
    select * from a left join b on a.id=b.id如果这个你做不出来,则只能先搞清楚这个,否则你根本不可能去理解你的那个问题。
      

  8.   

    您怎么总是让我做题啊 ...   是在考我内连接 和外连接的区别哈
    半斤白酒 + 若干啤酒 继续做题  come on come on
    select * from a inner join b on a.id=b.id 
    1 a1 1 b1select * from a left join b on a.id=b.id 
    1 a1 1 b1
    2 a2 null null
      

  9.   

    还竟然能做对。现在换一下A               B
    id  colA        id  colB
    1   a1          1   b1
    2   a2          3   b3请你列出
    select * from a inner join b on a.id>b.id
    select * from a left join b on a.id>b.id
      

  10.   

    A               B
    id  colA        id  colB
    1   a1          1   b1
    2   a2          3   b3drop table if exists a,b;
    create table a (id int, colA varchar(4));
    create table b (id int, colA varchar(4));
    insert into a values(1,'a1'), (2, 'a2');
    insert into b values(1,'b1'), (3, 'b3');
    select * from a inner join b on a.id=b.id;
    #  +------+------+------+------+
    #  | id   | colA | id   | colA |
    #  +------+------+------+------+
    #  |    1 | a1   |    1 | b1   |
    #  +------+------+------+------+
    select * from a left join b on a.id=b.id;
    #  +------+------+------+------+
    #  | id   | colA | id   | colA |
    #  +------+------+------+------+
    #  |    1 | a1   |    1 | b1   |
    #  |    2 | a2   | NULL | NULL |
    #  +------+------+------+------+我琢磨的好像没有问题哦....   谢谢您的回复
      

  11.   

    换个符号继续。select * from a inner join b on a.id>b.id
    select * from a left join b on a.id>b.id
    怕你醉眼朦胧,特用红色标出。
      

  12.   

    select * from a inner join b on a.id>b.id2   a2   3   b3
    select * from a left join b on a.id>b.id1   a1  null null2  a2   1   b1
    谢谢 指教
      

  13.   

    select 1 from t1 inner join t2 on t1.pk=t2.pk where t1.cx = '...';是先执行  t1.cx = '....' 的过滤 还是先执行 t1.pk=t2.pk 的过滤呢?
      

  14.   


    这个显然不对 a.id =2, b.id=3 并不符合  a.id>b.id
      

  15.   

    看你的数据库了,一般来说数据库会先根据索引来进行优化。会先进行 where t1.cx=
      

  16.   

    select * from a inner join b on a.id>b.id
    1 a1 null nullselect * from a left join b on a.id>b.id
    2 a2 1 b1
    由于超过了三次 ...   新注册了一个帐号  还是我 j_now
      

  17.   


    o  原来和索引有关哈 :)  我用的就是 mysqld mysql> select version();
    +------------------+
    | version()        |
    +------------------+
    | 5.1.41-debug-log |
    +------------------+
    1 row in set (0.00 sec)
      

  18.   

    还是没做对,你还是到mysql中看一下答案是什么。
      

  19.   

    drop table if exists a,b;
    create table a (id int, colA varchar(4));
    create table b (id int, colA varchar(4));
    insert into a values(1,'a1'), (2, 'a2');
    insert into b values(1,'b1'), (3, 'b3'); 
    select * from a inner join b on a.id>b.id;
    select * from a left join b on a.id>b.id ;mysql> select * from a inner join b on a.id>b.id;
    +------+------+------+------+
    | id   | colA | id   | colA |
    +------+------+------+------+
    |    2 | a2   |    1 | b1   |
    +------+------+------+------+
    1 row in set (0.00 sec)mysql> select * from a left join b on a.id>b.id ;
    +------+------+------+------+
    | id   | colA | id   | colA |
    +------+------+------+------+
    |    1 | a1   | NULL | NULL |
    |    2 | a2   |    1 | b1   |
    +------+------+------+------+
    2 rows in set (0.00 sec)我少些了一条 ...  但我心思是对的, 但少写了一行
      

  20.   

    A               B
    id  col        id  col
    1   2          1   2
    1   3          1   3
    1   4          1   4
    2   1          2   1
    2   3          2   3select * 
    from a left join b on a.id=b.id and a.cola<b.col结果是什么?
      

  21.   

    select *
    from a left join b on a.id=b.id and a.cola <b.col 
    先 a 再 b
    1 2 1 3
    1 2 1 4
    1 3 1 4
    1 4 null null
    2 1 2 3
    2 3 null null不知道是否正确,  感谢回复  我去 mysql 验证
      

  22.   

    正确。现在换成
    select * 
    from a left join b on a.id=b.id and a.cola <b.col
    where b.id is null
      

  23.   


    drop table if exists  a, b;
    create table a (id smallint, col smallint);
    create table b (id smallint, col smallint);
    insert into a values
    (1,2), (1, 3), (1,4), (2,1), (2,3);
    insert into b values
    (1,2), (1, 3), (1,4), (2,1), (2,3);
    select *
    from a left join b on a.id=b.id and a.col <b.col;
    #  +------+------+------+------+
    #  | id   | col  | id   | col  |
    #  +------+------+------+------+
    #  |    1 |    2 |    1 |    3 |
    #  |    1 |    2 |    1 |    4 |
    #  |    1 |    3 |    1 |    4 |
    #  |    1 |    4 | NULL | NULL |
    #  |    2 |    1 |    2 |    3 |
    #  |    2 |    3 | NULL | NULL |
    #  +------+------+------+------+
    bingo
      

  24.   

    select * 
    from a left join b on a.id=b.id and a.cola <b.col
    where b.id is null这个的结果应该能够理解吧。
      

  25.   

    这就是我问的问题....
    按照刚才的说法 先使用 where 过滤,那么结果就是
    1 2 null, null
    1 3 null, null
    1 4 null, null
    2 1 null, null
    2 3 null, null
      

  26.   

    刚才你是 a.id=b.id where a.c > ..而现在是 a.id<b.id where mysql 无法对这种进行优化!
      

  27.   


    drop table if exists  a, b;
    create table a (id smallint, col smallint);
    create table b (id smallint, col smallint);
    insert into a values
    (1,2), (1, 3), (1,4), (2,1), (2,3);
    insert into b values
    (1,2), (1, 3), (1,4), (2,1), (2,3);
    select *
    from a left join b on a.id=b.id and a.col <b.col
    where b.id is null;
    #  +------+------+------+------+
    #  | id   | col  | id   | col  |
    #  +------+------+------+------+
    #  |    1 |    4 | NULL | NULL |
    #  |    2 |    3 | NULL | NULL |
    #  +------+------+------+------+
      

  28.   

    如果你现在对
    select *
    from a left join b on a.id=b.id and a.col <b.col
    where b.id is null;没有问题。只是对为什么先执行 a left join b on a.id=b.id and a.col <b.col 再执行 where b.id is null; 有问题的话。可以说即使是 on a.id=b.id where a.c >xxx 这种语句,同样在语义上也是先 on a.id=b.id 然后再 where a.c >xxx , 但MYSQL会根据实际的表情况进行等价的优化.
      

  29.   

    select *
    from a left join b on a.id=b.id and a.cola <b.col
    where b.id is null 我理解的是 现使用 b.id is null 过滤,结果没有一个满足,所以 b 的过滤结果是  null
    而后按照 a left join b
    所以我返回的结果集应该是正确的啊  为什么 mysql 返回的不是我想的呢 ..... faint .... 
      

  30.   

    http://dev.mysql.com/doc/refman/5.1/zh/optimization.html
    7. 优化
    7.1. 优化概述
    7.1.1. MySQL设计局限与折衷
    7.1.2. 为可移植性设计应用程序
    7.1.3. 我们已将MySQL用在何处?
    7.1.4. MySQL基准套件
    7.1.5. 使用自己的基准
    7.2. 优化SELECT语句和其它查询
    7.2.1. EXPLAIN语法(获取SELECT相关信息)
    7.2.2. 估计查询性能
    7.2.3. SELECT查询的速度
    7.2.4. MySQL怎样优化WHERE子句
    7.2.5. 范围优化
    7.2.6. 索引合并优化
    7.2.7. MySQL如何优化IS NULL
    7.2.8. MySQL如何优化DISTINCT
    7.2.9. MySQL如何优化LEFT JOIN和RIGHT JOIN
    7.2.10. MySQL如何优化嵌套Join
    7.2.11. MySQL如何简化外部联合
    7.2.12. MySQL如何优化ORDER BY
    7.2.13. MySQL如何优化GROUP BY
    7.2.14. MySQL如何优化LIMIT
    7.2.15. 如何避免表扫描
    7.2.16. INSERT语句的速度
    7.2.17. UPDATE语句的速度
    7.2.18. DELETE语句的速度
    7.2.19. 其它优化技巧
      

  31.   

    好  我看到了 ch12,打算看完了 ch12 再看 ch7,恩 谢谢!!!!!!
    至于  数据库系统概论,21cn 的那本教材?  不想看了 没意思感觉
    我宁愿去看物化视图也不愿意看概论.... 其实您不觉得 ansi sql 设计的有问题吗  明显的面向过程编程.... 弄得人很那个,一个
    select 能搞定全天下的问题,这明显是内聚过高的表现,需要代码重构....只是个人的想法 ....  
      

  32.   

    good dream tonight, thx alot
      

  33.   

    左连接,当左有,右无时,右显示为null
    举例一个,就很明白了
    #   +---------+--------+---------+
    #   | article | dealer | price   |
    #   +---------+--------+---------+
    #   | 0001    | D      | 19.9500 |
    #   +---------+--------+---------+
    #   | 0001    | D      | 18.9500 |
    #   +---------+--------+---------+
    SELECT *
    FROM shop s1
    LEFT JOIN shop s2 ON s1.article = s2.article AND s1.price < s2.price
    这样子查询出来,S2是null,所以再通过后面的where
    就可以得到结果
    其实不难,很好理解,效率就不清楚了