Jump to content

combining 2 queries


esport

Recommended Posts

Hi Guys,

 

Not sure if this is possible. I have 2 tables. 1 table that is a set of categories. The other table is a set of products and categories. What I want is to list each category but then I want to know how many products are in that particular category. I know you can do 2 seperate queries in a loop, but if i have over 100 entries, then it would run the query 100 times. Can I do it so we can have the 1 query?

 

The tables look like this for example:

category(catid,name)

(1, Bikes)

(2, Cars)

products(catid,pid)

(1, car1)

(1, car2)

(1,car3)

(2,car1)

 

Thanks

 

Daniel

 

 

 

 

 

 

Link to comment
Share on other sites

I think you miss understood. Here is an example of the way I am doing it at the moment.

 

$res = mysql_query("SELECT * FROM categories",$server);
while($getCat=mysql_fetch_array($res)){
     $catid = $getCat[cat'id'];
     $res2 = mysql_query("SELECT * FROM products WHERE catid=$catid",$server);
     $numRows = mysql_num_rows($res2);
     echo $getCat['name]." has ".$numRows." products<br>";
}

 

Is it possible to cobine this into 1 query? So it doesn't run the second query every loop?

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.