Please help with this MySQL query

Discussion in 'MySQL' started by romic, Apr 12, 2007.

  1. #1
    Hello,

    I have the need to make a query to a MySQL database (2 tables in the database). To be easier to understand what I want the query to do, I'll explain the structure of the tables.

    Table1 contains a list of properties that a product can have and they are in the following form:

    IDprop | property
    1 | property1
    2 | property2
    3 | property3
    4 | property4
    5 | property5

    .... and so on

    Table2 contains products and details for each product. One of the fields in this table (table2) contains the product properties, which are stored in the following form " 1 ; 2 ; 4 ; 5 ; 7 ;" (the numbers are the IDprop of each property contained by this product)

    And now here's my question:
    How can I select the product properties in such a way that only those properties that are contained by at least one of the products would be shown? I need to select only these because after this I would make it (property name) a link and the opening page would show all the products that have this property.

    Thank you for any suggestions you might have.
     
    romic, Apr 12, 2007 IP
  2. druidelder

    druidelder Peon

    Messages:
    285
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Do an inner join.

    Select Table1.property From Table1 Inner Join Table2 On Table1.propID = Table2.propID
     
    druidelder, Apr 12, 2007 IP
  3. romic

    romic Peon

    Messages:
    81
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    druidelder,

    I thought about a JOIN but I'm not sure how this would work, because the product can contain (and in most cases it does contain) more than one property, so propID in Table2 (products table) contains something like " 1 ; 2 ; 4 ; 5 ; 7 ;" (propID are separated by ";")

    My ideia was to select all the "propID"s from Table2(products table) and put them all in one array, after this to use the function "array_unique()" and delete all the "propID"s that occur more than once in the array, and final point to select the propID names (those which IDs I have in the array) from Table1 (properties table)

    What do you think about this?
     
    romic, Apr 12, 2007 IP
  4. druidelder

    druidelder Peon

    Messages:
    285
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Sorry, I see what you mean now. That might work. If you change the delimiter to a , then you could use the IN clause.

    Select Distinct Table1.property From Table1 Where Inner Join Table2 On Table1.propID In Table2.propID

    Your other option is to have a third table.

    tblProduct
    productID int (pk)
    productName char(30)

    tblProperty
    propertyID int (pk)
    propertyName char(30)

    tblProductProperty
    prodPropID int (pk)
    productID int (fk)
    propertyID int (fk)
     
    druidelder, Apr 12, 2007 IP
  5. romic

    romic Peon

    Messages:
    81
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank you, it's not a problem for me changing the delimiter if you think this would help.

    Thanks. I'll try the solution you provided.
     
    romic, Apr 12, 2007 IP
  6. romic

    romic Peon

    Messages:
    81
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I have changed the delimiters to , but this still doesn't work :mad: :(
     
    romic, Apr 12, 2007 IP
  7. druidelder

    druidelder Peon

    Messages:
    285
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I wrote that badly.

    It would be something more like

    Select Distinct Table1.property From Table1 Inner Join Table2 On Table1.propID In(Table2.propID)

    Or possibly

    Select Distinct Table1.property From Table1 Where Table1.propID In(select Table2.propID from Table2)
     
    druidelder, Apr 12, 2007 IP
  8. romic

    romic Peon

    Messages:
    81
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    HI, I found this working, but they only give the first value from Table2.propID

    If the field Table2.propID contains "2,4,5,6,8" it will only return 2
     
    romic, Apr 12, 2007 IP
  9. druidelder

    druidelder Peon

    Messages:
    285
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Right, that gets you a list of properties that have a matching product. That is what you wanted on the first page. The next page where you click through to see the list of products will need a different query.

    Are you running the queries from the page or are you using stored procedures?

    The SQL will differ depending on the answer.
     
    druidelder, Apr 12, 2007 IP
  10. druidelder

    druidelder Peon

    Messages:
    285
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Also, if you used the where in() method you may want to add some data to the table to make sure that 1 is not considered to be in 10.
     
    druidelder, Apr 12, 2007 IP
  11. romic

    romic Peon

    Messages:
    81
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    If the field Table2.propID contains "2,4,5,6,8" it will only return 2 but I need it to return all the values "2,4,5,6,8" and any other that other products might contain
     
    romic, Apr 12, 2007 IP
  12. romic

    romic Peon

    Messages:
    81
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    I thought about this that's why my properties IDs start at 10 and finish 84 (there are 75 properties in total)
     
    romic, Apr 12, 2007 IP
  13. romic

    romic Peon

    Messages:
    81
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Yes, that gets me a list of properties that have a matching product. But I need a list of ALL properties that have a matching product. The query above only returns a few of them :confused:

    I know how to make the script for other pages - thanks for your concern - but I still need to find a solution for this.
     
    romic, Apr 12, 2007 IP
  14. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #14
    Hi,
    If I got you right, this might work...

    Select all properties from table1.

    Start a loop
    //Get the "IDprop" of each row individually, stored in "$id" for example, then:

    $sql=mysql_query("SELECT * FROM `table2` WHERE column_for_idprop LIKE '%$id :%' LIMIT 1");
    $num= mysql_numrows($sql);
    if($num>0)
    {
    //make a link here.
    }

    // end the loop.

    If you meant something else, sorry...
    Bye :)
     
    JEET, Apr 12, 2007 IP
  15. druidelder

    druidelder Peon

    Messages:
    285
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #15
    My problem was that I tested those in tables where the matching field did not have a list, just single number. So I made some tables to match yours and came up with this:

    SELECT DISTINCT aaProperty.propertyName
    FROM aaProperty
    INNER JOIN aaProduct ON aaProduct.properties LIKE '%' + CAST(aaProperty.propertyID AS varchar) + '%'

    aaProperty -> the properties table
    propertyName -> name of property from aaProperty
    propertyID -> id field for aaProperty
    aaProduct -> the products table
    properties -> comma delimited list of properties a product has
     
    druidelder, Apr 12, 2007 IP
  16. romic

    romic Peon

    Messages:
    81
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    You're saying you have tested this and it worked? It doesn't work for me :confused: so I'll guess I'll have to go for the solution with an additional table
     
    romic, Apr 12, 2007 IP
  17. druidelder

    druidelder Peon

    Messages:
    285
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Yes, it worked for me. What version of MySQL are you using? They have made updates to JOINs in almost every release. What works in newer releases doesn't always work in newer versions, and what works in older versions doesn't always work the same in newer versions. I thought what I wrote would work back to 4.0, but maybe not.

    Also, in the tables I created the propertyID is an int. If you are using a varchar, there would be no need for the CAST().
     
    druidelder, Apr 13, 2007 IP
  18. romic

    romic Peon

    Messages:
    81
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #18
    I'm using MySQL5 but have decided that I will use an additional table.
    Thanks a lot for your help.
     
    romic, Apr 13, 2007 IP