mysql - fetch records from a table to which it is having similar ids -
i have 2 tables like
uid name 11 cadman 12 clive 13 coleman 14 chester
and second table like
id pa1 pa2 pa3 pa4 4800 11 12 11 14 4801 11 12 13 14 4802 11 12 12 4973 12 13 14 6882 12 12 13 14 6883 11 12 14 6884 11 13 13 14 6885 11 13 13 14
i display names instead of ids can 1 me in query in advance
create / populate
create table nt ( -- name table uid int auto_increment primary key, name varchar(100) not null ); create table ( -- id table (sorry creativity @ low) id int primary key, pa1 int not null, pa2 int not null, pa3 int not null, pa4 int not null, -- in constraining kinda mood: constraint fk_pa1_nt foreign key (pa1) references nt(uid), constraint fk_pa2_nt foreign key (pa2) references nt(uid), constraint fk_pa3_nt foreign key (pa3) references nt(uid), constraint fk_pa4_nt foreign key (pa4) references nt(uid) ); insert nt (name) values ('kurt'),('burt'),('curt'),('dirt'),('lert'),('murte'),('qbert'); insert nt (name) values ('jim'),('kim'),('dim'),('sim'),('tim'),('cadman'),('bim'); insert it(id,pa1,pa2,pa3,pa4) values (4800,11,12,13,14); insert it(id,pa1,pa2,pa3,pa4) values (4801,3,5,7,9);
query
select it.id,nt1.name,nt2.name,nt3.name,nt4.name join nt nt1 on nt1.uid=it.pa1 join nt nt2 on nt2.uid=it.pa2 join nt nt3 on nt3.uid=it.pa3 join nt nt4 on nt4.uid=it.pa4 order it.id; +------+------+------+--------+------+ | id | name | name | name | name | +------+------+------+--------+------+ | 4800 | sim | tim | cadman | bim | | 4801 | curt | lert | qbert | kim | +------+------+------+--------+------+ 2 rows in set (0.00 sec)
Comments
Post a Comment