y1d2y3xyz 发表于 2013-2-4 19:42:02

SQL整理八

结构表:TabTest+-------+--------+|t_Code   t_Name ||1       a   ||2       b   ||11      c   |+-------+--------+SQL:+-----------------------------------------------------------+Create table TabTest(t_Code varchar(10),t_Name varchar(10));   insert into TabTest values('1','a');   insert into TabTest values('2','b');   insert into TabTest values('11','c');+-----------------------------------------------------------+(代码11表示为1的下级)结果表:+--------+--------+--------+|代码   名称    有无下级 |+--------+--------+--------+| t_Code | t_name | you_wu |+--------+--------+--------+| 1      | a      | you    | | 2      | b      | wu   | | 11   | c      | wu   |+--------+--------+--------+要求:1.通过一条语句,得到结果表的数据。+---------------------------------------------+|select tt1.t_Code,tt1.t_name,(               || case                                        || when exists (select 1 from TabTest tt2      |                                       | where tt2.t_code like CONCAT(tt1.t_code,'%')| | and tt2.t_code <> tt1.t_code )            || then 'you'                                  ||   else 'wu'                               || end                                       ||) as you_wu from TabTesttt1;               |+---------------------------------------------+
页: [1]
查看完整版本: SQL整理八