How do I use SQL to select rows that have > 50 related rows in another table? -
i've been trying find out how write query in sql.
what need find productnames (in products table) have 50 or more orders (which in order table).
only 1 orderid matched productname @ time when try count orderid's counts of them.
i can distinct productnames once add in orderid's goes having multiple productnames.
i need count number of customers (in order table) have ordered products.
i need serious asap! if me figure out how figure out awesome!
table: products `productname` in form of text 'grannysmith' table: orders `orderid` in form of '10222'..etc `custid` in form of 'smith'
assuming orders table has field relates products table named productid. sql translate to:
select p.productname, count(*) orders o join products p on o.productid = p.productid group p.productname having count(*) >= 50
the key in having
component of group clause. hope helps.
Comments
Post a Comment