Author Topic: How to modify this sql code  (Read 8646 times)

0 Members and 1 Guest are viewing this topic.

Offline wahban

  • Pre-Newbie
  • Posts: 5
    • View Profile
How to modify this sql code
« on: February 24, 2009, 10:09:46 PM »
Hi every body,  :)

can some one help me to modfy this sql to make it get data from the man category and it's sub categories without including the id of Each sub category  :?

this is my way
Code: [Select]
AND i.cat_id IN (31,30,28,25,23,22,20,19,35,15,16,18,24,26,32,34,27,33)

 I want the code to get the sub categories automaticly

Code: [Select]
$sql = "SELECT i.image_id, i.user_id, i.cat_id, i.image_name, i.image_thumb_file, i.image_rating, i.image_votes, c.cat_name, u.user_name
        FROM ".IMAGES_TABLE." i, ".CATEGORIES_TABLE." c, ".USERS_TABLE." u
        WHERE i.image_active = 1 AND i.cat_id NOT IN ($cat_id_sql) AND i.cat_id = c.cat_id  AND i.user_id = u.user_id $cat_match_sql  AND i.cat_id IN (31,30,28,25,23,22,20,19,35,15,16,18,24,26,32,34,27,33)
        ORDER BY i.image_rating DESC, i.image_votes DESC
        LIMIT 20";

Offline V@nо

  • Addicted member
  • ******
  • Posts: 1.223
    • View Profile
Re: How to modify this sql code
« Reply #1 on: February 24, 2009, 10:32:36 PM »
maybe something like that:
Code: [Select]
$sql = "SELECT i.image_id, i.user_id, i.cat_id, i.image_name, i.image_thumb_file, i.image_rating, i.image_votes, c.cat_name, u.user_name
         FROM ".IMAGES_TABLE." i, ".CATEGORIES_TABLE." c, ".USERS_TABLE." u
         WHERE i.image_active = 1 AND i.cat_id NOT IN ($cat_id_sql) AND i.cat_id = c.cat_id  AND i.user_id = u.user_id $cat_match_sql  AND (i.cat_id IN (31,30,28,25,23,22,20,19 ,35,15,16,18,24,26,32,34 ,27,33) OR c.cat_parent_id IN (31,30,28,25,23,22,20,19 ,35,15,16,18,24,26,32,34 ,27,33))
         ORDER BY i.image_rating DESC, i.image_votes DESC
         LIMIT 20";
Your first three "must do" before you ask a question:
If I asked you to PM me, I meant PM to my primary account, this account doesn't accept PMs.

Offline V@nо

  • Addicted member
  • ******
  • Posts: 1.223
    • View Profile
Re: How to modify this sql code
« Reply #2 on: February 24, 2009, 10:33:46 PM »
this method will not work for sub-sub-categories
Your first three "must do" before you ask a question:
If I asked you to PM me, I meant PM to my primary account, this account doesn't accept PMs.

Offline wahban

  • Pre-Newbie
  • Posts: 5
    • View Profile
Re: How to modify this sql code
« Reply #3 on: February 24, 2009, 10:55:55 PM »
Thaaaaaaanks aloooooot V@nо   :D

It works fine