-1

I have two tables with users and I want to select the users from the first table which do not exist in the second. Can you help me?

When I use the code

Select t1.user_name From t1 Inner Join t2 On t1.user_name != t2.user_name;

I get all the users many times (actually as the number of the users - 1).

user2515973
  • 17
  • 1
  • 5
  • 2
    You will need to review this web page: http://www.w3schools.com/sql/sql_join.asp - and if you are still confused you will need to put up an example so that the community can help you – Quintin Balsdon Aug 07 '15 at 07:30
  • possible duplicate of [mysql: select all items from table A if not exist in table B](http://stackoverflow.com/questions/4660871/mysql-select-all-items-from-table-a-if-not-exist-in-table-b) – Alex Tartan Aug 07 '15 at 07:32

2 Answers2

1

Use a LEFT JOIN instead like

Select t1.user_name From t1 left join t2 
On t1.user_name = t2.user_name
where t2.user_name is null;
Rahul
  • 76,197
  • 13
  • 71
  • 125
-1

You can use EXISTS like this

SELECT t1.user_name FROM t1 WHERE NOT EXISTS (SELECT 1 FROM t2 WHERE t1.id = t2.id)

This example assumes that you have some sort of ID on the tables that represents the primary key and foreign key.

Not sure how are your tables designed, but having the same info (user_name) in more than one table is considered as duplication of data. To fix this, you should read about Database normalization

Zavael
  • 2,383
  • 1
  • 32
  • 44
  • Instead of copying the answer from the question this duplicates, you should flag the question as duplicate. Your answer is exactly the first part of http://stackoverflow.com/a/4660915/1181435 – Alex Tartan Aug 07 '15 at 08:15
  • I wrote it, now I see it is really the same.. should I delete it? – Zavael Aug 07 '15 at 08:43