Computer problems,Computer help
*AX SOFT>>>Programming & Design

SQL Query Help Needed :)?


Ok brief overview. For this part there are 2 main tables: user_cat and category.

category table is simply the names of categories and a corresponding is eg:

catid catname
1 fruit
2 veg
3 sweets
etc

The user_cat table holds user id and a cat id to assign them a or multipl categories eg

user_cat
userid catid
1 2
1 3
2 1
3 1
3 2

etc etc.




Now the SQL I want is to select the categories a user belongs to based on the user id. So here is what I have currently:

SELECT CATNAME
FROM CATEGORY, USER_CAT, SITE_USER
WHERE USER_CAT.USERID = #url.id# AND CATEGORY.CATID = USER_CAT.CATID

Which in fairness when tested does give a list but it lists all categories a cust is linked with about ten times and using UNIQUE in the SQL does not work. How can I make it so that the category name is only selected once seeing as the UNIQUE isn't working.

(Another part of this issue in further details :P)

Ultimately what I want to do is print out a list of the categories of a user. As it stands now with the current setup described above, the print statement is printing out only one of the users categories even though it should loop through the resultset from the SQL.

I imagine though that once the previous problem is fixed everything else should follow suit but this is just to give you more info on the problem.

You only described one join: between category and user_cat. You need to join site_user too.

SELECT CATNAME
FROM CATEGORY, USER_CAT, SITE_USER
WHERE USER_CAT.USERID = #url.id# AND CATEGORY.CATID = USER_CAT.CATID
AND USER_CAT.USERID = SITE_USER.USERID

Without that last join, SQL does a cartesian product between the current user's categories and ALL entries in the user table.

In fact, you don't even need to include the site_user table because you're not even using it!

SELECT CATNAME
FROM CATEGORY, USER_CAT
WHERE USER_CAT.USERID = #url.id# AND CATEGORY.CATID = USER_CAT.CATID

You are missing a join. The site_user table needs to be joined to the user_cat table.

You can either remove the site_user table from your FROM clause or add "AND user_cat.user_id = site_user.user_id" to your WHERE clause.

Tags
  General - Computers & Internet   Software   Security   Programming & Design   Facebook   Flickr   Google   MSN   MySpace
Related information
  • Photoshop help....please it is urgent!?

    Hey there. You'll need to flatten your image by merging all the layers together onto one layer. If you want to have a working copy of your photo to make further adjustments, I suggest saving...

  • Javascript- can someone write this code for me?

    document.body.bgcolor is an outmoded usage - should be document.body.style.backgroundColor. Also, providing "White" as the first value (default) ensures that a change event will be fired ...

  • How do you load textures on Adobe Photoshop 7?

    You can create your own in Photoshop or download them from websites. They can be jpeg files or even psd files. To use them in Photoshop, they need to be copied to the following folder: C:\Pro...

  • My PHP form isn't sending to my email?

    thats your entire script? email me..... nothing seems right here at all....also try changing the file name to config.inc.php.....if that dont work email me with more details silent...

  • T -SQL tutorial sites?

    Here are a few sites to look over: ...

  • Have you ever use Transperent note Book?

    yes i heard about it.

    ...
  • Can anyone with programming, specifically C++, experience help me? (little needed) What's wrong in this script

    int main(void) ( <===== problem That parenthesis needs to be changed to a "{" instead ....called a "left curly brace" .... it is next to the "P" key Change...

  • Is the C++ for dummies book a good way to start learning C++?

    I own the book and had read much of it as a 14-year-old. I felt like it had an awkward pace. It starts off by saying a lot of stuff I didn't understand (like the history of GNU C++, how to p...

  •  

    Categories--Copyright/IP Policy--Contact Webmaster