database - SQL - Join two tables but only show specific value -
alright i've created database called news contains few tables different information in them. have 2 table's 1 named employee's , other table called article's.
receive employee tabel name(the names of employee's) , title. , article table show type of article every employee has been on , writen since connected through articleid. specific articleid show example numer 2. here example of output show.
titel name articleid redirector jonas 2 journalist clark 2 journalist louise 2
this how far i've goten sql code cant work, error i'm getting says p.employee not exsist.
select p.titel, p.name, ap.articleid employee p join article ap on p.employee = ap.article ap.articleid in (2);
some on appreciated.
edit ----------------- how table structure looks. apologize inconvenience caused.
table employee
employeeid name username pass titel phonenumer 1 clark xxxxxx journalist 12356465 2 louise aaaaaa b journalist 45648984 3 jonas bbbbbb c redirector 489489448
table article
articleid kategoriid preamble body headlines published 1 1 dwadwad 2 2 qweqw 3 3 dwqdqw
the way query looks now, if tables connected through employee.employee , article.article.
the way describe it, link on articleid, sounds should be:
select p.titel, p.name, ap.articleid employee p inner join article ap on p.articleid = ap.articleid ap.articleid in (2); -- or ` = 2`
but suspect employee can write multiple articles, in case make more sense article has employeeid, , employeeid defines relationship:
select p.titel, p.name, ap.articleid employee p inner join article ap on p.employeeid = ap.employeeid ap.articleid in (2); -- or ` = 2`
though it's reasonable multiple employees co-author article. if want have possibility 1 employee writes multiple articles, need junctions table, , query this:
select p.titel, p.name, ap.articleid employee p inner join employeearticle ea on ea.employeeid = p.employeeid inner join article ap on ap.articleid = ea.articleid ap.articleid in (2); -- or ` = 2`
in last case, employeearticle, contain 2 fields, employeeid , articleid, allow link multiple employees multiple articles (n..m relation). if don't need fields article itself, leave out join article
, write:
where ea.articleid in (2); -- or ` = 2`
after all, id there in junction table.
Comments
Post a Comment