u_company表,主键id,有parentid字段,parentid对应的就是主键id,比如某一行,parentid为1,代表它的父级是主键为1的那一列,我现在查询id为1908和它下级的所有信息
select * from u_company where id in (select distinct(id) from u_company where parentid = 1908 union select id from u_company where id = 1908)
这里内层查询除了select distinct(id) from u_company where parentid = 1908 union select id from u_company where id = 1908,union后面必须用一个查询吗,不用查询,直接跟1908怎么解决
一般是分两次查询的,先拿到主键,然后拿到下级信息。
select * from u_company where id in (select distinct(id) from u_company where parentid = 1908 union select id from u_company where id = 1908)
这句SQL语句,如果你知道parentid=1908,直接用:
select distinct(id),* from u_company where parentid = 1908
这样不行?
select distinct(id) from u_company where parentid = 1908 union select '1908'
这样可以吗,最好用uion all,重复的也能落得一起,这里貌似就有重复吧