Go Back   Forum Care Forums > Development Reference Area > MySQL Discussion

Reply
 
LinkBack Thread Tools Display Modes
o/t mysql query
Old
  (#1)
Guest
Guest
 
Posts: n/a
Default o/t mysql query - 06-04-2007, 07:48 AM

This should be simple but I'm missing something. If at all possible can
somebody please hint how I should be writing the following query
(mySQL).
I need to query the total count of products that meet all the selected
categories.

-----------------------------------
cat | product
-----------------------------------
1 1
2 1
1 2
1 3
2 3

Q: so how many products are ***igned to both cat 1&2

Thanks in advance.
Mark Dyer
NZ

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Re: o/t mysql query
Old
  (#2)
Captain Paralytic
Guest
 
Posts: n/a
Default Re: o/t mysql query - 06-04-2007, 07:48 AM


EMAIL REMOVED wrote:

> This should be simple but I'm missing something. If at all possible can
> somebody please hint how I should be writing the following query
> (mySQL).
> I need to query the total count of products that meet all the selected
> categories.
>
> -----------------------------------
> cat | product
> -----------------------------------
> 1 1
> 2 1
> 1 2
> 1 3
> 2 3
>
> Q: so how many products are ***igned to both cat 1&2
>
> Thanks in advance.
> Mark Dyer
> NZ

Does the 3 in the product column in the last row mean that there are 3
products for that row?

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: o/t mysql query
Old
  (#3)
Guest
Guest
 
Posts: n/a
Default Re: o/t mysql query - 06-04-2007, 07:48 AM

I think (but not tested)

SELECT SUM(product) FROM <TABLE> GROUP BY cat



Captain Paralytic wrote:
> EMAIL REMOVED wrote:
>
> > This should be simple but I'm missing something. If at all possible can
> > somebody please hint how I should be writing the following query
> > (mySQL).
> > I need to query the total count of products that meet all the selected
> > categories.
> >
> > -----------------------------------
> > cat | product
> > -----------------------------------
> > 1 1
> > 2 1
> > 1 2
> > 1 3
> > 2 3
> >
> > Q: so how many products are ***igned to both cat 1&2
> >
> > Thanks in advance.
> > Mark Dyer
> > NZ

> Does the 3 in the product column in the last row mean that there are 3
> products for that row?


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: o/t mysql query
Old
  (#4)
Felix Geerinckx
Guest
 
Posts: n/a
Default Re: o/t mysql query - 06-04-2007, 07:48 AM

"EMAIL REMOVED" <EMAIL REMOVED> wrote in
news:EMAIL REMOVED ups.com:

> Q: so how many products are ***igned to both cat 1&2


USE test;
DROP TABLE IF EXISTS foo;
CREATE TABLE foo (cat TINYINT UNSIGNED NOT NULL, product TINYINT UNSIGNED
NOT NULL);
INSERT INTO foo (cat, product) VALUES
(1, 1), (2, 1), (1, 2), (1, 3), (2, 3);

SELECT COUNT(*)
FROM (
SELECT product
FROM foo
WHERE cat IN (1, 2)
GROUP BY product
HAVING COUNT(DISTINCT cat) = 2
) T;

--
felix
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Contact Us - Forum Care Forums - Archive - Top