2

I have 2 tables, as given below. How can I check which data_entry's information are not in the task_detail table.

data_entry
+----+----------+----------+
| id | firstname| lastname |
+----+----------+----------+
| 1  | Anil     | Chaudhari|
+----+----------+----------+
| 2  | Sunil    | Chaudhari|
+----+----------+----------+
| 3  | Pranil   | Chaudhari|
+----+------+--------------+

task_detail
+----+-------+--------------+--------+
| id | de_id | field_name   | amount |
+----+-------+--------------+--------+
| 1  | 1     | firstname    | 0.1    |
+----+-------+--------------+--------+
| 2  | 1     | lastname     | 0.1    |
+----+-------+--------------+--------+

I just want

data_entry
+----+----------+----------+
| id | firstname|lastnamer |
+----+----------+----------+
| 2  | Sunil    | Chaudhari|
+----+----------+----------+
| 3  | Pranil   | Chaudhari|
+----+------+--------------+
Anil Chaudhari
  • 801
  • 14
  • 30
  • [Subqueries with EXISTS or NOT EXISTS](http://dev.mysql.com/doc/refman/5.7/en/exists-and-not-exists-subqueries.html) – bansi Dec 10 '15 at 04:41

5 Answers5

2

Maybe this will work:

SELECT * FROM data_entry 
WHERE  id NOT IN (SELECT  de_id FROM task_detail) 
Jibin Balachandran
  • 3,381
  • 1
  • 24
  • 38
2

In most cases joins are faster than sub queries ref: sql join vs sub query, so you can also use left join:

select de.* from data_entry de left join task_detail td on td.de_id = de.id where td.id is null
Community
  • 1
  • 1
Chetan Ameta
  • 7,696
  • 3
  • 29
  • 44
1

Using subquery.

SELECT * FROM data_entry WHERE id NOT IN (SELECT de_id FROM task_detail)

Using left join

SELECT de.* FROM data_entry de LEFT JOIN task_detail td ON de.id=td.de_id WHERE td.field_name IS NULL
Jain
  • 854
  • 5
  • 15
1

You can try this:

SELECT * FROM data_entry 
WHERE NOT EXISTS
(SELECT * FROM task_detail WHERE de_id=data_entry.id) 
Jibin Balachandran
  • 3,381
  • 1
  • 24
  • 38
  • @Anil Chaudhari Check this one also – Jibin Balachandran Dec 10 '15 at 05:31
  • 1
    Using Subqueries : Total results 1464 Showing rows 0 - 24 (1464 total, Query took 0.0170 seconds.) Using Left Join : Total results 1464 Showing rows 0 - 24 (1464 total, Query took 0.0020 seconds.) Using NOT EXISTS : Showing rows 0 - 24 (1464 total, Query took 0.2390 seconds.) – Anil Chaudhari Dec 10 '15 at 05:43
0
select t1.* from data_entry t1 join data_entry t2 on t1.id = t2.de_id where t1.id !=  t2.de_id
ashish mulani
  • 197
  • 1
  • 14