0

I have two tables with phone numbers.

I want check if there is new rows in addresbook table that not exist in users table.

As I am newbie with SQL, is this query correct?

SELECT * 
FROM addressbook
WHERE NOT EXISTS (
  SELECT Phone1
  FROM users 
  WHERE addressbook.phone = users.phone1
)

EDIT: I user MySQL with PHPMyAdmin Interface, sorry for not to specify before

Alex Deiwor
  • 117
  • 3
  • 9

2 Answers2

5

Seems OK but I would do it like this.

select a.* from addressbook a
left outer join users u on a.phone = u.phone1
where
(u.phone1 is null)

This is simpler and probably faster.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
1

In SQL Server your query is

SELECT * from addressbook WHERE Phone NOT in (SELECT Phone1 from users)
Vishal Patel
  • 953
  • 5
  • 11