0

Just wanted to ask if this is possible or a way to determine the strings that is not on my table. for example

select name from table_person where name IN('name1','name2','name3')

  • scenario is name1 and name2 is available on my table but what I want to display is name3, since I want to know what are the things I haven't added.
Lego_blocks
  • 301
  • 2
  • 6
  • 15

3 Answers3

2

Just playing around with the worst approach (may be).

Not Recommended

SELECT 
 suppliedArgTable.name 
FROM 
(
 SELECT 'name1' AS name
 UNION 
 SELECT 'name2' 
 UNION 
 SELECT 'name3' 
) AS suppliedArgTable

LEFT JOIN 

table_person TP ON TP.name = suppliedArgTable.name 
WHERE TP.name IS NULL;

http://sqlfiddle.com/#!9/edcbe/2/0

1000111
  • 13,169
  • 2
  • 28
  • 37
  • yep, that's kinda like that, but what if I have 100 arguments to check and 14 of them is missing from my table. – Lego_blocks Sep 01 '16 at 18:57
  • I just showed the other way around. But I don't recommend this. Better do this kind of job in application logic. – 1000111 Sep 01 '16 at 19:00
  • I honestly can't think of a way around this. Your master set of data you want to compare against isn't in the database. So first have to some how get it there... either a union query load it into a table first and then do this. – xQbert Sep 01 '16 at 19:03
  • I agree. I don't advocate this @xQbert – 1000111 Sep 01 '16 at 19:13
0

NOT IN combined with reversing your query is a solution.

With the 'list' ('name1','name2','name3') in a (temporary) table e.g. temp_list and with the data in table_person the query would be:

select name from temp_list 
where name not in (
    select distinct(name) from table_person
)

distinct removes doubles. (see also MySQL: Select only unique values from a column)

Community
  • 1
  • 1
Bert
  • 119
  • 8
0
SELECT name_field
FROM   (VALUES('name1'),
              ('name2'),
              ('name3'),
              ('name4'),
              ('name5')) V(name_field)
EXCEPT
SELECT name_field
FROM   name_table

You can use a temporary table to hold a list of all names and find non-matching names with EXCEPT.

Vivek Kumar
  • 2,625
  • 2
  • 25
  • 33