Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi, I have two tables: Product.t

Status
Not open for further replies.

4181944

Programmer
Aug 6, 2002
9
NZ
Hi,
I have two tables:
Product.tbl
-------------------------
ProductID | ProductName |
------------|--------------
1 | CD-ROM |
----------|--------------
2 | RAM |
----------|--------------
3 | Monitor |
-------------------------
Category.tbl
-------------------------
ProductID | Category |
----------|--------------
1 | 52X |
----------|--------------
1 | 36X |
----------|--------------
1 | 24X |
----------|--------------
2 | 512MB |
----------|--------------
2 | 256MB |
----------|--------------
1 | 48X |
----------|--------------
3 | 19" |
----------|--------------
3 | 17" |
----------|--------------

Can someone tell me how to write a query that can generate the below table:
-------------------------------------------
ProductID | ProductName | Num-of-Category |
----------|-------------|------------------
1 | CD-ROM | 4 |
----------|-------------|--------------------
2 | RAM | 2 |
----------|-------------|-------------------
3 | Monitor | 2 |
-------------------------------------------
My query is:

Select *, (Select count(*) from Category where ProductID=1) as Num-of-Category
from Product

this will generate like:
-------------------------------------------
ProductID | ProductName | Num-of-Category |
----------|--------------------------------
1 | CD-ROM | 4 |
----------|--------------------------------
2 | RAM | 4 |
----------|--------------------------------
3 | Monitor | 4 |
-------------------------------------------

which is uncorrect for row2 and row 3. Please help to write this query.

Many thanks

Richard
 
Select
*,
(Select count(*) from Category
where ProductID = Product.ProductID) as Num-of-Category
from Product

not tested, but should work

hope this helps
LFCfan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top