Select value that does not exist in another column of the same table, MySQL Database Not Exists Clause

DoubleClickOnThis

What I'm trying to do here is to select all contract_id that does not exist in the prev_contract_id column and whereby the end date is earlier than a date.

Here is my database table called contract, and the values are in (contract id, contract type, start date, end date, prev contract id, staff id) columns

insert into contract values('co-0000001', 'Monthly', '2014-01-01', '2014-02-01','null', 'staff3');
insert into contract values('co-0000002', 'Yearly', '2013-07-07', '2014-07-06','null', 'staff3');
insert into contract values('co-0000003', 'Monthly', '2014-03-20', '2014-04-19','co-0000001', 'staff4');
insert into contract values('co-0000004', 'Yearly', '2014-02-27', '2015-03-27','null', 'staff4');
insert into contract values('co-0000005', 'Ad-hoc', '2014-02-27', '2015-03-27','null', 'staff4');
insert into contract values('co-0000006', 'Yearly', '2013-02-27', '2014-02-27','null', 'staff4');

Here is my SQL statement

select contract_id from contract 
where not exists(select prev_contract_id from contract where prev_contract_id <> 'null') 
and end_date < '2014-05-05';

The results of each individual select statements

select prev_contract_id from contract where prev_contract_id <> 'null';

returns me co-0000001 contract_id, and

select contract_id from contract where end_date < '2014-05-05';

returns me co-0000001, co-0000003, co-0000006. I want to eliminate co-0000001 from the selection and only return me co-0000003 and co-0000006.

It works when i run them separately, but when i combine them together using the NOT EXISTS clause, I was not able to get my results. I do not want to use IN as I heard it's more time consuming or inefficient.

Mureinik

You are using the not exists operator wrong. This operator means that for every row in the outer select statement, the inner select is evaluated, and only if it returns no rows the row from the outer query is returned.

If you examine the inner query you're using:

select prev_contract_id from contract where prev_contract_id <> 'null'

You will see that it has nothing to do with the outer query. Since there exists rows where prev_contract_id <> 'null', no row from the outer query is ever returned.

What you're missing is an additional condition that ties the inner query with the contract_id of the outer one:

SELECT contract_id 
FROM   contract a
WHERE  NOT EXISTS(SELECT prev_contract_id 
                  FROM   contract b
                  WHERE  b.prev_contract_id <> 'null' AND
                         b.prev_contract_id = a.contract_id -- The missing relationship
                 ) AND
       end_date < '2014-05-05';

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL select from a table and check in another table if the same value exists

From Dev

select query from one table to check same value exist in column, mysql select query

From Dev

Select column datediff value use for another column in same select in mysql

From Dev

SQL Select - Calculated Column if Value Exists in another Table

From Dev

Get count if column value does not exist in another table

From Dev

Update value in a column based on another column in the same table in MYSQL

From Dev

mysql query to insert the value of a column with respect to another column in same table

From Dev

Select rows with same id but different value in another column in a table

From Dev

select rows in mysql having another column with same value

From Dev

If exists select column from another table

From Dev

If duplicates exist, select the value based on another column

From Dev

mysql SELECT. If value in WHERE does not exist, then SELECT based on another (value) WHERE

From Dev

need to select the data that does not exist in another table

From Dev

How to select data from another table if it does not exist on the other table in mysql

From Dev

Update column in Table A from Table B where value does not exist in distinct result from Table B in MYSQL

From Dev

How to select items from a table based on one value if another value does not exists? (eloquent/sql)

From Dev

How can I change the field value in a column to 'other' if the value does not exist in another table?

From Dev

How to use paginate() with a having() clause when column does not exist in table

From Dev

select value from table that exist on another table condition by id value

From Dev

Select max column value when column value does not exist?

From Dev

SQL Server : how to select the rows in a table with the same value on a column but some exact values on another column for the grouped rows

From Dev

Sql how to select from a table that does not exist in another table

From Dev

delete row from table where column does not exist in another table

From Dev

Column value must exist in non primary column of another table

From Dev

Column value must exist in non primary column of another table

From Dev

mysql insert into table select from another database

From Dev

order rows in a table if column value exists in another table

From Dev

Between two tables how does one SELECT the table where the id of a specific value exists mysql (duplicate)

From Dev

mysql - Select unique column based on max value of another column in a different table

Related Related

  1. 1

    MySQL select from a table and check in another table if the same value exists

  2. 2

    select query from one table to check same value exist in column, mysql select query

  3. 3

    Select column datediff value use for another column in same select in mysql

  4. 4

    SQL Select - Calculated Column if Value Exists in another Table

  5. 5

    Get count if column value does not exist in another table

  6. 6

    Update value in a column based on another column in the same table in MYSQL

  7. 7

    mysql query to insert the value of a column with respect to another column in same table

  8. 8

    Select rows with same id but different value in another column in a table

  9. 9

    select rows in mysql having another column with same value

  10. 10

    If exists select column from another table

  11. 11

    If duplicates exist, select the value based on another column

  12. 12

    mysql SELECT. If value in WHERE does not exist, then SELECT based on another (value) WHERE

  13. 13

    need to select the data that does not exist in another table

  14. 14

    How to select data from another table if it does not exist on the other table in mysql

  15. 15

    Update column in Table A from Table B where value does not exist in distinct result from Table B in MYSQL

  16. 16

    How to select items from a table based on one value if another value does not exists? (eloquent/sql)

  17. 17

    How can I change the field value in a column to 'other' if the value does not exist in another table?

  18. 18

    How to use paginate() with a having() clause when column does not exist in table

  19. 19

    select value from table that exist on another table condition by id value

  20. 20

    Select max column value when column value does not exist?

  21. 21

    SQL Server : how to select the rows in a table with the same value on a column but some exact values on another column for the grouped rows

  22. 22

    Sql how to select from a table that does not exist in another table

  23. 23

    delete row from table where column does not exist in another table

  24. 24

    Column value must exist in non primary column of another table

  25. 25

    Column value must exist in non primary column of another table

  26. 26

    mysql insert into table select from another database

  27. 27

    order rows in a table if column value exists in another table

  28. 28

    Between two tables how does one SELECT the table where the id of a specific value exists mysql (duplicate)

  29. 29

    mysql - Select unique column based on max value of another column in a different table

HotTag

Archive