#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    2
    Rep Power
    0
    Hey Guys,
    Im using MySQL, and what I want to do is show only the first occurence records with distinct id values. Let me give an example, if I had the following table,

    >create Table products_categories (
    > product_id int ....,
    > category_id int ...,
    > )

    I have two other tables, "products" and "categories" and since a single
    product can belong to multiple categories, I get a side effect when
    displaying all products, in that a single product can be displayed multiple
    times (according to how many different categories it belongs to). For
    example, if product "Apple-Orange juice", which has a product_id 1, belongs
    to Apples category(category_id=3) and Oranges category(category_id=5), this
    will result in two records in the products_categories table, looking like

    "1,3" and "1,5"

    Now when I do a select that needs to gather category, products and
    products_categories fields, I get multiple records with the same
    product_id, so I end up displaying the same product multiple times, when
    instead I only want to display it once. So thus I want to get only one
    occurence each of each distinct product_id. Now, Im solving this problem
    right now via a php script, but was wondering if there was an SQL(or MySQL)
    solution to this problem?

    Thanks everyone,

    Christian
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2000
    Location
    Den Laeghe Landen
    Posts
    107
    Rep Power
    15
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by fromdc:
    Now, Im solving this problem
    right now via a php script, but was wondering if there was an SQL(or MySQL)
    solution to this problem?
    [/quote]

    yes there is. In stead of typing
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>SELECT product_id FROM products_categories .... [/code]
    use this one:
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>SELECT DISTINCT product_id FROM products_categories .... [/code]

    That should do it.

    Greetz,
    EyE


    ------------------
    --
    ...tiktak...tiktak...
    --
    ...Tommyknockers, Knocking On Your Door...

Similar Threads

  1. question on storing/editing dates with php/mysql
    By quadrant6 in forum PHP Development
    Replies: 1
    Last Post: January 26th, 2004, 02:55 AM
  2. Php/mysql Distinct List Menu Question
    By mrpilz in forum PHP Development
    Replies: 3
    Last Post: January 9th, 2004, 01:40 PM
  3. Javascript question mark question?
    By tajmiester in forum JavaScript Development
    Replies: 3
    Last Post: November 30th, 2003, 06:36 PM
  4. General PHP/MySQL question
    By ashabi in forum PHP Development
    Replies: 5
    Last Post: November 26th, 2003, 08:44 AM
  5. Radio Buttons
    By Ulysses Freeman in forum HTML Programming
    Replies: 4
    Last Post: November 15th, 2003, 10:57 PM

IMN logo majestic logo threadwatch logo seochat tools logo