0

I am joining multiple tables into a single query. I need to do a partial match on values in an IN statement. Here is an example.

SELECT DISTINCT 
    am.id AS id, 
    am.flagged AS flagged, 
    am.name AS name, 
    am.type AS type, 
    am.file AS file, 
    am.s3_tag AS s3_tag, 
    am.low_s3_tag AS low_s3_tag 
FROM accounts_media am 
    LEFT JOIN accounts_location_media alm ON am.id = alm.media_id 
    LEFT JOIN accounts_location al ON al.id = alm.location_id 
    LEFT JOIN accounts_person_media apm ON am.id = apm.media_id 
    LEFT JOIN accounts_person ap ON ap.id = apm.person_id 
    LEFT JOIN accounts_event_media_record aemr ON am.id=aemr.media_id 
    LEFT JOIN accounts_medianote_media_record amma ON am.id=amma.media_id 
    LEFT JOIN accounts_medianote amn ON amma.medianote_id=amn.id 
WHERE 
    am.account_id = '1234' 
    AND am.flagged = FALSE 
    AND ('Da' IN (SELECT first_name FROM accounts_person WHERE account_id = '1234') 
    AND ('Rob' IN (SELECT first_name FROM accounts_person WHERE account_id = '1234') 

In the

AND ('Da' IN (SELECT first_name FROM accounts_person WHERE account_id = '1234')

statement there are values that say 'Dan', 'Daniel', etc. in the table. There is also 'Rob' and 'Robert'. I need that statement to make sure and name that contains 'Da' AND any name that contains 'Rob' from that table. Is there a way to do this?

So a record can be linked to multiple people in the accounts_person table. So lets say I have three records.

Record One: A person named Dan is attached to the record.

Record Two: A person named Robert is attached to the record.

Record Three: A person named Dan and a person named Robert are attached to the record.

I want the query to only return Record Three because it has the match of 'Da' and 'Rob'.

4

2 に答える 2

3

Try this:

WHERE 
    am.account_id = '1234' 
    AND am.flagged = FALSE 
    -- AND ( ap.first_name LIKE '%Da%' OR ap.first_name LIKE '%Rob%')
    AND EXISTS 
        ( SELECT 1
            FROM accounts_person apx
           WHERE apx.first_name LIKE '%Da%'
             AND apx.account_id = am.account_id
        )
    AND EXISTS 
        ( SELECT 1
            FROM accounts_person apy
           WHERE apy.first_name LIKE '%Rob%'
             AND apy.account_id = am.account_id
        )
于 2013-02-21T04:26:34.400 に答える
1

I'm not sure, but I think you want a like statement, with a wild card after the Da.

SELECT DISTINCT 
    am.id AS id, 
    am.flagged AS flagged, 
    am.name AS name, 
    am.type AS type, 
    am.file AS file, 
    am.s3_tag AS s3_tag, 
    am.low_s3_tag AS low_s3_tag 
FROM accounts_media am 
    LEFT JOIN accounts_location_media alm ON am.id = alm.media_id 
    LEFT JOIN accounts_location al ON al.id = alm.location_id 
    LEFT JOIN accounts_person_media apm ON am.id = apm.media_id 
    LEFT JOIN accounts_person ap ON ap.id = apm.person_id 
    LEFT JOIN accounts_event_media_record aemr ON am.id=aemr.media_id 
    LEFT JOIN accounts_medianote_media_record amma ON am.id=amma.media_id 
    LEFT JOIN accounts_medianote amn ON amma.medianote_id=amn.id 
WHERE 
    am.account_id = '1234' 
    AND am.flagged = FALSE 
    AND (accounts_person.first_name like '%Da%'
    OR accounts_person.first_name like '%Rob%') 
    AND accounts_person.account_id = '1234'
于 2013-02-21T02:59:08.840 に答える