mysql - querying primary keys from a foreign key -
i'm new mysql , struggling querying or referencing primary key's foreign keys specified in table. can please explain how in simple terms, i've searched bit hard understand considering majority of others have allot more mysql knowledge me.
let's assume these tables:
create table if not exists customer( custid int auto_increment, custlname varchar(30), custadd varchar(100), custsuburb varchar(30), custpcode varchar(4), custstate varchar(20), custphone varchar(10), primary key (custid) )engine = innodb; create table if not exists tour( dailytourid int, tourdate date, tourtime time, tourname varchar(30), tourdriverid int, tourbusid varchar(2), primary key (dailytourid, tourdate), ) engine = innodb; create table if not exists tourcustlink( tourcustlinkid int auto_increment, tourid int, tourdate date, customerid int, primary key (tourcustlinkid), foreign key (tourid, tourdate) references tour(dailytourid, tourdate), foreign key (customerid) references customer(custid) ) engine = innodb;
i aware pretty bad example, lets want show custlname, custadd, custphone , tourdates each customer. how accomplish this?
join
them:
select c.custlname, c.custadd, c.custphone, t.tourdate, t.tourname, ... tourcustlink tl inner join customer c on tl.customerid = c.custid inner join tour t on tl.tourid = t.dailytourid , tl.tourdate = t.tourdate;
you might need use outer join
s (left join
or right join
) instead of inner join
, in case want include unmatched rows.
for more information join
types see article: