sql - Joining tables back to themselves in MySql -
let's have 3 tables:
user
table:
create table `user` ( `id` int(11) unsigned not null auto_increment, `name` varchar(50) default null, `loc` int(11) default null, `doc` int(11) default null, primary key (`id`) ) engine=myisam auto_increment=3 default charset=latin1;
location
table:
create table `location` ( `id` int(11) unsigned not null auto_increment, `name` varchar(50) default null, primary key (`id`) ) engine=myisam auto_increment=4 default charset=latin1;
and document
table:
create table `document` ( `id` int(11) unsigned not null auto_increment, `maintainer` int(11) default null, primary key (`id`) ) engine=myisam auto_increment=2 default charset=latin1;
i can pull user info , it's corresponding location
, document
info following query:
select * `user` left join `location` on user.loc = location.id left join `document` on user.doc = document.id;
the location
info referenced information doesn't refer other rows in other tables. document
table, however, contains maintainer
field directly corresponds user
in user
table. field encapsulates user
information, , not give me actual user
data.
is there not way of querying tables such data maintainer
returns actual user
data opposed id
?
select u.name user_name, m.name maintainer_name, l.name location_name user u left outer join document d on d.id = u.doc left outer join user m on m.id = d.maintainer left outer join location l on l.id = u.loc
Comments
Post a Comment