mysql - query only returns one value and it should return 2 -
i have following tables.
create table `customer` ( `cid` varchar(10) character set latin1 not null default '', `name` varchar(40) character set latin1 not null default '', `city` varchar(40) character set latin1 not null default '', `state` varchar(40) character set latin1 not null default '', primary key (`cid`) ) engine=innodb default charset=latin1 collate=latin1_bin; create table `lineitem` ( `lid` varchar(10) not null default '', `oid` varchar(10) not null default '', `pid` varchar(110) not null default '', `number` int(11) default null, `totalprice` decimal(10,2) default null, primary key (`lid`), key `order id` (`oid`), constraint `order id` foreign key (`oid`) references `orderitem` (`oid`) on update cascade ) engine=innodb default charset=latin1; create table `orderitem` ( `oid` varchar(10) not null default '', `cid` varchar(10) not null default '', primary key (`oid`), key `cid` (`cid`), constraint `cid` foreign key (`cid`) references `customer` (`cid`) on update cascade ) engine=innodb default charset=latin1; create table `product` ( `pid` varchar(10) not null default '', `productname` varchar(40) default '', `price` decimal(10,2) default null, primary key (`pid`) ) engine=innodb default charset=latin1;
what i've been trying in query run can following:
list products bought customers of newark
list products ordered customers of newark
for #5, tried query:
select product.productname customer c inner join orderitem o on c.cid = o.cid inner join lineitem line on o.oid = line.oid inner join product product on line.pid = product.pid c.city = 'newark' having count(product.productname) > 1;
but returns 1 value , should return 2 (unless not using properly).
for #6 understand concept don't know how "subtract tables" in sql.
the goal of first question list common items purchased newark. if person bought items x, y , z , person b bought w, v, , y, query return "item y".
i guess comment answer.
having count(product.productname) > 1;
having requires group function correctly it's filter on aggregate , aggregates require group by. 90% of database engines have returned error explicitly stating requires group by...but mysql prefers wrong thing instead of return error (it's why got 1 row...mysql did group of whatever felt like). add group (i assume on product name have here) , should work.
Comments
Post a Comment