Difficult MYSQL query

Discussion in 'Databases' started by wryfhk22, Apr 20, 2011.

  1. #1
    I have two tables in a database:

    Table 1 - files:

    column 1 - ID
    column 2 - deleted



    Table 2 - seminar_files

    column 1- seminar_id
    column 2 - file_id



    ID from table 1 and file_id from table 2 are the column that bridges these together. So for example, I may have a file with an ID of "hakurocks" in table 1, and that same 'hakurocks' may appear in table 2. It may appear zero times, one time or many times, but will have a different seminar_id for each time it appears.

    I want to select all the IDs from from table 1 that aren't in table 2 when seminar_id is equal to 18 (for example). On top of this, I only want to select the files from table one where deleted is set to zero.

    Anybody got an idea on how to put together this mysql query? I've tried a bunch of different ways, and I either end up with ALL the files from table one, or none!
     
    wryfhk22, Apr 20, 2011 IP
  2. SamTech

    SamTech Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hello,

    Can you try following queries and let me know if its works :

    select Table1.ID where Table1.ID <> Table2.ID AND Table2.seminar_id = 18 FROM Table1, Table2;

    Select Table2.file_id where Table.Id=Table2.seminar_ID AND Table1.deleted=0 FROM Table1, Table2;
     
    SamTech, Apr 21, 2011 IP
  3. rdthewall

    rdthewall Active Member

    Messages:
    1,225
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    80
    #3
    Hope this helps:
     
    rdthewall, Apr 25, 2011 IP
  4. Seo Myth

    Seo Myth Greenhorn

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #4
    let use JOIN mysql statement replace for WHERE to solve it
     
    Seo Myth, Apr 30, 2011 IP
  5. annie0207

    annie0207 Greenhorn

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #5
    Have you tried : SELECT * FROM ((SELECT * FROM Table1 WHERE Table1.ID NOT IN (SELECT Table2.ID FROM Table2)) AS Join_Table) WHERE Table1.deleted = 0
    or break it into 2 query statement:
    SELECT * FROM Table1 WHERE Table1.ID NOT IN (SELECT Table2.ID FROM Table2) AS Join_Table & SELECT * FROM Join_Table WHERE ID = 0
    ?
    Goodluck!
     
    annie0207, Jul 11, 2011 IP