MySQL NOT IN from another column in the same table

后端 未结 5 2044
天涯浪人
天涯浪人 2021-02-09 00:32

I want to run a mysql query to select all rows from a table films where the value of the title column does not exist anywhere in all the values of anot

相关标签:
5条回答
  • 2021-02-09 00:45
    SELECT * 
    FROM films 
    WHERE title NOT IN (SELECT collection FROM films where collection is not null);
    

    SQLFiddle: http://sqlfiddle.com/#!2/76278/1

    0 讨论(0)
  • 2021-02-09 00:52
    CREATE TABLE IF NOT EXISTS `reservation_tables` (
      `res_table_id` int(10) NOT NULL AUTO_INCREMENT,
      `res_table_name` int(10) NOT NULL,
      `date_time` varchar(20) NOT NULL,
      `partyhall_id` int(10) NOT NULL,
      `flag` enum('0','1') NOT NULL DEFAULT '0',
      PRIMARY KEY (`res_table_id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;
    
    
    INSERT INTO `reservation_tables` (`res_table_id`, `res_table_name`, `date_time`, `partyhall_id`, `flag`) VALUES
    (1, 1, '2014-08-17 12:00 am', 7, '1'),
    (2, 2, '2014-08-17 12:00 am', 7, '1'),
    (3, 3, '2014-08-18 12:00 am', 8, '1'),
    (4, 4, '2014-08-18 12:00 am', 8, '1'),
    (5, 1, '2014-08-25 12:00 am', 12, '1'),
    (6, 2, '2014-08-25 12:00 am', 12, '1'),
    (7, 3, '2014-08-20 12:00 am', 23, '1'),
    (8, 4, '2014-08-20 12:00 am', 23, '1');
    

    Ι had to select available table name for matching date_time

    Example select available table_name where date_time = 2014-08-18 12:00 am.

    solution query is: im sure this works well

    SELECT distinct res_table_name FROM reservation_tables WHERE `res_table_name` NOT IN   
    (SELECT `res_table_name` FROM reservation_tables where `date_time` = '2014-08-17 12:00 am')
    
    0 讨论(0)
  • 2021-02-09 01:07

    Try this please:

    • SQLFIDDLE DEMO

    Query:

    select a.id, a.planid
    from one a
    left join one b
    on a.planid <> b.iid
    where not (b.iid is null)
    group by b.id
    ;
    

    Results: based on the sample table I used.

    ID      PLANID
    t15     1
    j18     2
    

    EDIT TO ADD : HERE WITH OP SCHEMA

    select b.id, b.title
    from opschema b
    inner join opschema a
    on b.title <> a.collection
    or b.collection <> a.title
    group by b.id 
    ;
    

    OP SHCEMA SQLFIDDLE DEMO

    ID  TITLE
    2   Film 1
    3   Film 2
    
    0 讨论(0)
  • 2021-02-09 01:09

    Have you tried using NOT EXISTS:

    SELECT * 
    FROM films f1
    WHERE NOT EXISTS (SELECT collection 
                      FROM films f2
                      WHERE f1.title = f2.collection);
    

    See SQL Fiddle with Demo

    If you want to use IN then you will want to look for values that are NOT NULL:

    SELECT * 
    FROM films 
    WHERE title NOT IN (SELECT collection 
                        FROM films
                        WHERE collection is not null);
    

    See SQL Fiddle with Demo

    The result for both is:

    | ID |  TITLE |   COLLECTION |
    ------------------------------
    |  2 | Film 1 |       (null) |
    |  3 | Film 2 | Collection 1 |
    

    The problem with your current query is that -- stealing from @Quassnoi's answer here:

    Both IN and NOT IN return NULL which is not an acceptable condition for WHERE clause.

    Since the null value is being returned by your subquery you want to specifically exclude it.

    0 讨论(0)
  • 2021-02-09 01:11

    Another option using an outer join

    SELECT f.* 
    FROM films f LEFT OUTER JOIN films ff
    ON f.title = ff.collection
    WHERE ff.collection IS NULL
    
    0 讨论(0)
提交回复
热议问题