mysql - Get all Items attached to sellerId - SQL -
when execute query 1 item attached sellerid instead of 2. know how can say?
select name of item , re seller each item belongs re seller. rating higher 4?
current query:
select items.name, sellers.name items inner join sellers on items.id=sellers.id rating > 4 order sellerid
the query tables inc. data:
create table sellers ( id integer not null primary key, name varchar(30) not null, rating integer not null ); create table items ( id integer not null primary key, name varchar(30) not null, sellerid integer references sellers(id) ); insert sellers(id, name, rating) values(1, 'roger', 3); insert sellers(id, name, rating) values(2, 'penny', 5); insert items(id, name, sellerid) values(1, 'notebook', 2); insert items(id, name, sellerid) values(2, 'stapler', 1); insert items(id, name, sellerid) values(3, 'pencil', 2);
you've got wrong join, here's corrected query;
select items.name, sellers.name items inner join sellers on items.sellerid=sellers.id rating > 4 order sellerid
you're joining on id = id, want sellerid = id
notice in table definition item.sellerid field joins seller.id
create table items ( id integer not null primary key, name varchar(30) not null, sellerid integer references sellers(id) );
Comments
Post a Comment