Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

SQL Question

  • 16-01-2004 5:08pm
    #1
    Closed Accounts Posts: 1,651 ✭✭✭


    Hey folks,

    Trying to get this query working for the last few hours.
    In my DB app I've got a MAIN table and a MAIN_PROPERTY table.
    They look something like this
    MAIN
    ====
    ID
    CONTENT
    ETC
    ETC
    
    
    MAIN_PROPERTY
    ============
    ID
    MAIN_ID
    PROP_NAME
    PROP_VALUE
    

    The MAIN.ID and MAIN_PROPERTY.MAIN_ID values link up.
    I want to be able to find MAINs where PROP_NAME = 'something' AND PROP_VALUE = 'something_value'
    AND PROP_NAME = 'something_else' AND PROP_VALUE = 'something_else_value'

    etc etc.

    I think im in over my head here :)

    Can anyone shed any light on this for me please? Cheers!

    MySQL 4.0.1 BTW


Comments

  • Registered Users, Registered Users 2 Posts: 68,317 ✭✭✭✭seamus


    Ok, what I'm thinking is this (but I dunno if it's right).

    SELECT * FROM MAIN_PROPERTY WHERE (PROP_NAME = 'something' OR PROP_NAME = 'something_else') AND (PROP_VALUE = 'something_value' OR PROP_VALUE = 'something_else_value')

    But I don't know if you can group boolean statments like that :)


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    No that doesn't work for some reason. I think it's because MAIN_PROPERTY.PROP_NAME can't be two things at once.
    That's why if you use an OR in between the two main clauses then it works.


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    Was looking on the MySQL.com website there (the documentation is really good and very well presented)
    Found some guy who had posted some similar code.

    Here's what I managed to come up with, seems to be working :)
    SELECT M.* FROM MAIN AS M
    LEFT JOIN MAIN_PROPERTY mp1 ON mp1.PROP_NAME = 'something' AND mp1.PROP_VALUE = 'something_value'
    LEFT JOIN MAIN_PROPERTY mp2 ON mp2.PROP_NAME = 'something_else' AND mp2.PROP_VALUE = 'something_else_value'
    .
    .
    .
    .
    LEFT JOIN MAIN_PROPERTY mp[n] ......etc
    WHERE M.ID = mp1.MAIN_ID
    

    Don't really know (or care at this stage) how efficient it is but it works! I can go out now :D


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    One small alteration,
    for each join you do, you have to add a
    AND M.ID = mp[n].MAIN_ID

    Just incase someone else has the same problem :)


  • Registered Users, Registered Users 2 Posts: 15,443 ✭✭✭✭bonkey


    Thats the right solution, but it can be made a bit neater/more efficient :

    You should change the LEFT JOIN to an INNER JOIN in each case, and also move the M.ID=Mp[n].ID comparison into each of the JOIN clauses, rather than applying them at the end in a WHERE clause.

    Thusly (for two descriptions only) :
    SELECT  M.* 
      FROM  MAIN AS M
      INNER JOIN MAIN_PROPERTY mp1 
        ON  mp1.MAIN_ID = M.ID
        AND mp1.PROP_NAME = 'something' 
        AND mp1.PROP_VALUE = 'something_value'
      INNER JOIN MAIN_PROPERTY mp2 
        ON  mp2.MAIN_ID = M.ID
        AND mp2.PROP_NAME = 'somethingElse' 
        AND mp2.PROP_VALUE = 'somethingElse_value'
    

    jc


  • Advertisement
  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    Thanks very much!


Advertisement