Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 08-18-2004, 03:46 AM   #1 (permalink)
kitrak2004
Registered User
 
Join Date: Aug 2004
Posts: 2
kitrak2004 is on a distinguished road
Mysql nested query help!

I need help in following query.

I am creating a database for Library listing for my college.

There are categories , sub - categories, sub sub - categories and so on.

What i want is what is similar at hotscripts.com . In PHP category, it shows number of listings inside it. Similarly, i wanted a mysql query which would directlt provide me number of listings inside it.

I have 3 tables

_______________________
One : stores the book categories
catid -> every book category has an unique ID
superset -> stores the parent category

For Ex.: If Entertainment category has catid = 10
then Sports category which belongs to Entertainment category has superset = 10


`category` (
`catid` bigint(6) NOT NULL default '0',
`name` varchar(60) NOT NULL default '',
`description` varchar(255) NOT NULL default '',
`superset` bigint(6) NOT NULL default '0',
`addable` tinyint(1) NOT NULL default '1',
UNIQUE KEY `catid` (`catid`)
) TYPE=MyISAM COMMENT='listing of all categories ';

_______________________
Two : Links the listings to the category they belong to.

`list_cat` (
`listid` bigint(6) NOT NULL default '0',
`catid` bigint(6) NOT NULL default '0'
) TYPE=MyISAM COMMENT='shows which listing is in which category';

_______________________
Three : Listing description.
`listing` (
`title` varchar(50) NOT NULL default '',
`listid` bigint(6) NOT NULL default '0',
PRIMARY KEY (`listid`)
) TYPE=MyISAM COMMENT='listing details';




I can do that using PHP loops , but it takes up lot of processor time and makes the local server slow. I guess this can be made easier of the server using some MYSQL command itself.

If someone could help, it would be very greatfull.
kitrak2004 is offline   Reply With Quote
Old 08-18-2004, 04:15 AM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,139
Belisarius is on a distinguished road
I'm not sure I followed exactly what you wanted to do, but let's see if I understand . . .

I know in PostgreSQL, you have the "count" function, that'll give you a count of the number of results. I assume MySQL has a similar function.

So just create a query for the for a category (alter this to taste, it's a pseudo-query), "SELECT count(l.*) FROM listing l, list_cat lc, category c WHERE l.listid=lc.listid AND lc.catid=c.catid AND c.name=<variable>;", where <variable> is insterted into the query in PHP. That should return you the number of the listings in a given category name.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-18-2004, 04:25 AM   #3 (permalink)
kitrak2004
Registered User
 
Join Date: Aug 2004
Posts: 2
kitrak2004 is on a distinguished road
no, what i want is if suppose there is an Entertainment category

Sports : 10
Television : 20
Movies : 15

So in first page when user has not gone into Entertainment category, I'm required to show a link with number of listings inside Entertainment category.

For above example , link would be " Entertainment (45) " , similar to what you see at www.hotscripts.com

PHP(10,902)

Hope, i made myself clear. Please help me out!

Kitrak 2004
kitrak2004 is offline   Reply With Quote
Old 08-18-2004, 04:44 AM   #4 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,139
Belisarius is on a distinguished road
I'm not sure it's possible to do that in MySQL. It sounds like you need some sub-selects, which if memory serves me don't exist in MySQL. Maybe in the beta versions it does. I know PostgreSQL supports them.

Basically, you'd need to query using the count function inside a query that returns all the categories.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-18-2004, 06:33 AM   #5 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
probably not the answer you want to hear, but how about adding a 'count' field to your category table. you would need to create the logic to increment and decrement the count of all higher level categories when you add or remove a listing.
Code:
`category` (
`catid` bigint(6) NOT NULL default '0',
`count` int(10) NOT NULL default '0',
`name` varchar(60) NOT NULL default '',
`description` varchar(255) NOT NULL default '',
`superset` bigint(6) NOT NULL default '0',
`addable` tinyint(1) NOT NULL default '1',
UNIQUE KEY `catid` (`catid`)
) TYPE=MyISAM COMMENT='listing of all categories ';
__________________
Mike
sde is offline   Reply With Quote
Reply

Bookmarks

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
mysql query to populate an array dubsonic PHP 2 06-20-2003 08:07 AM
foreach() while() for() or new mysql query? nemesis PHP 6 06-10-2003 06:57 PM
and on to mysql .. sde Linux / BSD / OS X 2 01-18-2003 07:39 PM


All times are GMT -8. The time now is 08:19 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting