Forum Moderators: coopster

Message Too Old, No Replies

just beyond my mortal self

sorry about that stupid subject line

         

maxi million

4:52 pm on Jul 14, 2005 (gmt 0)

10+ Year Member




Please Help! i am stuck with something in the middle of something and dont know how to go about it.
i have a table which is like i have shown below and i need to get something out of it. (btw abcdd is for example and got nothing to do with it)

id cat id subcat id item
-- ------- --------- ----
1 1 2 abcdd
2 1 1 abcdd
3 1 1 abcdd
4 1 1 abcdd
5 1 1 abcdd
6 2 3 abcdd
7 1 1 abcdd
8 1 2 abcdd
9 1 1 abcdd
10 1 1 abcdd
11 2 4 abcdd
12 1 2 abcdd
13 2 4 abcdd
14 1 3 abcdd
15 1 1 abcdd
16 1 3 abcdd
17 1 1 abcdd
18 2 4 abcdd
19 1 3 abcdd
20 2 4 abcdd

i need to find out:
1) for which subcat id there is most number of entries where the cat id is given (also the number)
2) which is the last entry for a given cat id
looking at it i know for cat id = 1 the last entry is row 19 and for cat id = 2 its row 20. and where cat id = 2 i know sub cat id 4 has most entries. but how do i do it?

all i know is that there needs to be loops but when i set out to write them, i just got lost somewhere and deleted all id written :( and im left with this

function WhichSubcatHasMostEntries($CatID)
{
}

if its of any use, ive got another table, where im storing the subcat id and cat id, in which subcat id is primary and unique.
please help me with this
thanks in adavance

Anyango

5:45 pm on Jul 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey There

this is Too much Simple..

Please reply with your Database Table Design with all field names and table names

and i ll write you your queries for that function within minutes.

Cheers

Kami

arran

6:16 pm on Jul 14, 2005 (gmt 0)

10+ Year Member



Rather than doing this in your application, it would make more sense to use the database.

If your table is of the form:

category (id, cat_id, subcat_id, item)

Then 1) would be something like:

select subcat_id, count(subcat_id) as 'total' from category where cat_id = $cat_id group by subcat_id order by total limit 1

And for 2)

select max(id) from category where cat_id = $cat_id

arran.

jd01

1:38 am on Jul 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if its of any use, ive got another table, where im storing the subcat id and cat id, in which subcat id is primary and unique.

Your initial post is a little vague, but I would use something similar to this...

You could add a col to the table with the unique id's 'subcat_cnt'. Initially you would have to insert the values (counts) manually, but then you could automate it.

When you update a category and/or sub category add an update to the subcat_cnt for a running total. Depending on how many categories and entries you have this could save quite a bit of time.

EG
UPDATE yourtable SET subcat_cnt=subcat_cnt+1 WHERE cat="$yourcategory"
(or something to this effect depending on the exact structure of your tables)

Now there is a running total for the categories, by subcategory, so rather than adding and sorting the full table, you can make category based selections from the table you just added the col to:

$sql="SELECT subcat FROM yourtable WHERE cat=$cat ORDER BY subcat_cnt DESC";

This will give you the subcat with the largest number of entries for the category selected.

**query stuff**

then find the last entry from the main table:
$last_entry = mysql_fetch_array($sql);

$sql2="SELECT * FROM main_table WHERE subcat=$last_entry['subcat'] ORDER BY id DESC LIMIT 1";

**query stuff**

Finally, you can output all the information:
while($result=mysql_fetch_array($sql2)) {
**do what you want with the stuff you just got**
}

There are a number of ways to solve this problem, I just prefer to keep things as short as possible when returning information, and by doing the counts on the way in they will be stored and can then be returned in a shorter period of time.

In this case it appears finding a category number and ordering by a count would be faster than finding, grouping and counting multiple cols to find the one with the most entries.

Hope this helps.

Justin

maxi million

5:24 am on Jul 15, 2005 (gmt 0)

10+ Year Member



thanks a lot all of you. now the picture is clearer to me. very useful comments, but havent tested the solutions yet. cant wait to get back to work...

maxi million

2:17 pm on Jul 16, 2005 (gmt 0)

10+ Year Member



thanks again arran and jd. i found your help very helpful. arran's solution is working great. i had to choose his over jd's for the reason that changing the db structure was not an option, there are too much data already. but ive got the picture, and when im onto something else ill surely keep in mind the problem i faced this time while designing the db.

thanks again for all the help