sql - SQLite select statement pulls all values although it is not supposed to -
i have these 3 tables:
create table "item" ( "itemid" integer primary key , "itemname" varchar(25), buyerid integer references buyer(buyerid), sellerid integer references seller(sellerid) ); create table buyer( buyerid integer primary key unique not null, fname varchar(25), lname varchar(25), itemid integer, foreign key (itemid) references item(itemid) ); create table seller( sellerid integer primary key unique not null, fname varchar(25), lname varchar(25), itemid integer, foreign key (itemid) references item(itemid) ); item table has: 1|poly|1|1 2|jute|2|2 3|salt|3|3 buyer table has: 1|buyer1|polybuyer|1 2|buyer2|jutebuyer|2 3|buyer3|saltbuyer|3 sellertable has: 1|seller1|polyseller|1 2|seller2|juteseller|2 3|seller3|saltseller|3
now want select query want itemname , first , last names of relevant buyers , sellers. suppose want list of buyers , sellers salt. query:
select buyer.fname, buyer.lname, itemname, seller.fname, seller.lname buyer, seller, item item.itemid = 3;
i expect return:
buyer3|saltbuyer|salt|seller3|saltseller
but instead get:
buyer1|polybuyer|salt|seller1|polyseller buyer1|polybuyer|salt|seller2|juteseller buyer1|polybuyer|salt|seller3|saltseller buyer2|jutebuyer|salt|seller1|polyseller buyer2|jutebuyer|salt|seller2|juteseller buyer2|jutebuyer|salt|seller3|saltseller buyer3|saltbuyer|salt|seller1|polyseller buyer3|saltbuyer|salt|seller2|juteseller buyer3|saltbuyer|salt|seller3|saltseller
so messing up? bad database design or bad query or both? thank in advance.
you need use join
combine 3 tables based on primary key-foreign key relation, example :
select buyer.fname, buyer.lname, itemname, seller.fname, seller.lname item inner join seller on item.itemid = seller.itemid inner join buyer on item.itemid = buyer.itemid item.itemid = 3;
Comments
Post a Comment