我有一张表,表结构如下:
CREATE TABLE `dict` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `dict_name` varchar(64) NOT NULL COMMENT '字典名称',
  `dict_type` int(6) NOT NULL COMMENT '数据字典类型',
  `dict_parent_id` int(11) DEFAULT NULL COMMENT '父ID',
  `dict_status` int(1) NOT NULL DEFAULT '1' COMMENT '0--不可用 1--可用',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='字典表'dict_type 是已知的有四种类型的值:1、2、3、4,并且依次为父子级关系
我想对 dict_name 字段进行查询,比如说 dict_name like '%马%'
就把相关的数据结果列出来,但是数据结果一定要以最子级为单位,且显示的 dict_name 包含至最父级
模拟数据:
id        dict_name           dict_type           dict_parent_id          dict_status
1        '宝马'               1                      0                      1
2        '7系列'              2                      1                      1
3        '2011宽'             3                      2                      1
4        '骏马奔驰'           4                      3                      1
5        '万马奔腾'           4                      3                      1
6        '马自达'             1                      0                      1
7        '快马加鞭'           3                      23                     1
8        '马上'               4                      30                     1
9        'test1'              1                      0                      1
10       'test2'              2                      9                      1
11       'tes马t3'            3                      10                     1
12       'test4'              4                      11                     1
12       'test5'              4                      11                     1
模拟数据有点不完善,但是能保证子级对应的 dict_parent_id  一定会有数据。
想要的结果示例(只要最子级 ID 和 dict_name 的组合即可):
4        '宝马7系列2011宽骏马奔驰'   
5        '宝马7系列2011宽万马奔腾'   
6        '马自达'
7        '快马加鞭'
12       'test1test2tes马t3test4'
13       'test1test2tes马t3test5'我本人是 Java 开发者,如果能提出Java方面的解决方案也可;
存储过程、伪代码、解决方案、思路,什么的都行。
致谢。

解决方案 »

  1.   

    mysql> select * from dict;
    +----+-----------+-----------+----------------+-------------+
    | id | dict_name | dict_type | dict_parent_id | dict_status |
    +----+-----------+-----------+----------------+-------------+
    |  1 | 宝马      |         1 |              0 |           1 |
    |  2 | 7系列     |         2 |              1 |           1 |
    |  3 | 2011宽    |         3 |              2 |           1 |
    |  4 | 骏马奔驰  |         4 |              3 |           1 |
    |  5 | 万马奔腾  |         4 |              3 |           1 |
    |  6 | 马自达    |         1 |              0 |           1 |
    |  7 | 快马加鞭  |         3 |             23 |           1 |
    |  8 | 马上      |         4 |             30 |           1 |
    |  9 | test1     |         1 |              0 |           1 |
    | 10 | test2     |         2 |              9 |           1 |
    | 11 | tes马t3   |         3 |             10 |           1 |
    | 12 | test4     |         4 |             11 |           1 |
    | 13 | test5     |         4 |             11 |           1 |
    +----+-----------+-----------+----------------+-------------+
    13 rows in set (0.00 sec)mysql> select id,dict_name from dict d
        -> where not exists (select 1 from dict where dict_parent_id=d.id)
        -> and dict_name like '%马%';
    +----+-----------+
    | id | dict_name |
    +----+-----------+
    |  4 | 骏马奔驰  |
    |  5 | 万马奔腾  |
    |  6 | 马自达    |
    |  7 | 快马加鞭  |
    |  8 | 马上      |
    +----+-----------+
    5 rows in set (0.01 sec)mysql>
      

  2.   

    为什么没有 记录8,其dict_type 为4,最子级,dict_name like '%马%'