MySQL----一个表查询到的值作为条件查询同一个表的值

表简单结构

CREATE TABLE IF NOT EXISTS table_school(    
    school_id smallint unsigned NOT NULL AUTO_INCREMENT COMMENT '校区id',
    city_pid smallint unsigned NOT NULL COMMENT '父级校区所在市id',
    school_name varchar(32) NOT NULL DEFAULT '' COMMENT '名称',
    school_intro varchar(255) NOT NULL DEFAULT '' COMMENT '简介',
    KEY(city_pid),
    PRIMARY KEY(school_id)
) ENGINE=innoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 COMMENT '校区表';

SQL

select a.*, (select school_name from table_school  where a.city_pid=school_id) as city_name from table_school as a
where a.school_id = :school_id;

你可能感兴趣的:(DB)