Skynet

          ---------- ---------- 我的新 blog : liukaiyi.cublog.cn ---------- ----------

            BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
            112 Posts :: 1 Stories :: 49 Comments :: 0 Trackbacks
          問(wèn)題描述:
          dic_flow   (id,status_id,status_name)
          post_status(id,pre_status_id,post_status_id)
          關(guān)系: dic_flow.status_id-(1.1)>post_status.pre_status_id,post_status_id
          要結(jié)果: pre_status_id,pre_status_name,post_status_id,post_status_name


          create table  dic_flow(id int,status_id int , status_name varchar(20));
          create table  post_status(id int ,pre_status_id int ,post_status_id int);
          insert into dic_flow values(1,1,'google');
          insert into dic_flow values(2,3,'baidu');
          insert into dic_flow values(3,8,'yahoo');
          insert into post_status values(1,8,3);

          select
              tp.pre_status_id 
          as pre_status_id,
              ( 
          select td.status_name from dic_flow  td where td.status_id=tp.pre_status_id) as pre_status_name ,
              tp.post_status_id 
          as post_status_id,
              ( 
          select td.status_name from dic_flow  td where td.status_id=tp.post_status_id) as post_status_name
          from post_status tp;

          +---------------+-----------------+----------------+------------------+
          | pre_status_id | pre_status_name | post_status_id | post_status_name |
          +---------------+-----------------+----------------+------------------+
          |             8 | yahoo           |              3 | baidu            |
          +---------------+-----------------+----------------+------------------+
          1 row in set (0.00 sec)
          Skynet(309290723) 21:11:57
          子查詢(xún)(性能優(yōu)化版)
          select
            tp.pre_status_id 
          as pre_status_id,
            td1.status_name 
          as pre_status_name ,
            tp.post_status_id 
          as post_status_id,
            td2.status_name 
          as post_status_name
          from post_status tp , dic_flow td1 , dic_flow td2
          where tp.pre_status_id=td1.status_id and tp.post_status_id=td2.status_id ;
          Skynet(309290723) 21:12:08
          +---------------+-----------------+----------------+------------------+
          | pre_status_id | pre_status_name | post_status_id | post_status_name |
          +---------------+-----------------+----------------+------------------+
          |             8 | yahoo           |              3 | baidu            |
          +---------------+-----------------+----------------+------------------+
          Skynet(309290723) 21:16:35
          再?lài)Z叨句
          左連(性能再優(yōu)化版)
          select
            tp.pre_status_id 
          as pre_status_id,
            td1.status_name 
          as pre_status_name ,
            tp.post_status_id 
          as post_status_id,
            td2.status_name 
          as post_status_name
          from post_status tp
           
          left join dic_flow td1 on tp.pre_status_id=td1.status_id
           
          left join dic_flow td2 on tp.post_status_id=td2.status_id  ;



          整理 www.aygfsteel.com/Good-Game
          posted on 2008-10-21 22:05 劉凱毅 閱讀(1335) 評(píng)論(2)  編輯  收藏 所屬分類(lèi): database

          Feedback

          # re: sql 語(yǔ)句筆記 2008-10-22 20:56 楊?lèi)?ài)友
          用inner join好像也可以  回復(fù)  更多評(píng)論
            

          # re: sql 語(yǔ)句筆記 2008-10-23 14:23 劉凱毅
          呵呵
          當(dāng)然 左連 , 右連 , 內(nèi)連 語(yǔ)句都相同
            回復(fù)  更多評(píng)論
            

          主站蜘蛛池模板: 镇赉县| 镇沅| 绿春县| 河源市| 清远市| 通河县| 玉溪市| 永新县| 黔西县| 富裕县| 连云港市| 玉山县| 富平县| 商洛市| 庆城县| 台东市| 东乌| 惠州市| 克东县| 晋州市| 凭祥市| 濮阳县| 平邑县| 磴口县| 普格县| 富顺县| 章丘市| 滦平县| 宝丰县| 黄浦区| 内丘县| 新晃| 姜堰市| 亚东县| 翼城县| 古田县| 丹寨县| 青龙| 侯马市| 平湖市| 微山县|