I have two tables that are identical in fields. I want to query names in table2
that are not in table1
. Both tables have name
field as unique (primary key).
Here are the info. of my database design: My query is:
SELECT `table2`.`name` FROM `mydatabase`.`table2`, `mydatabase`.`table1`
WHERE `table2`.`name` NOT IN (SELECT `table1`.`name` FROM `mydatabase`.`table1`)
AND table2`.`name` NOT LIKE 'xyz%';
The output of SHOW CREATE TABLE <table name>:
For table1
:
table1, CREATE TABLE `table1` (
`name` varchar(500) NOT NULL,
`ip` varchar(500) DEFAULT NULL,
`type` varchar(500) DEFAULT NULL,
`grade` varchar(500) DEFAULT NULL,
`extended_ip` text,
PRIMARY KEY (`name`),
UNIQUE KEY `mydatabase_name_UNIQUE` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
And table2
:
tabl2, CREATE TABLE `table2` (
`name` varchar(500) NOT NULL,
`ip` varchar(500) DEFAULT NULL,
`type` varchar(500) DEFAULT NULL,
`grade` varchar(500) DEFAULT NULL,
`extended_ip` text,
PRIMARY KEY (`name`),
UNIQUE KEY `mydatabase_name_UNIQUE` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
The output of EXPLAIN <my query>
:
# id, select_type, table, partitions, type, possible_keys, key, key_len, ref, rows, filtered, Extra
1, PRIMARY, table1, , index, , mydatabase_name_UNIQUE, 502, , 17584, 100.00, Using index
1, PRIMARY, table2, , index, , mydatabase_name_UNIQUE, 502, , 46264, 100.00, Using where; Using index; Using join buffer (Block Nested Loop)
2, SUBQUERY, table1 , index, PRIMARY,mydatabase_name_UNIQUE, mydatabase_name_UNIQUE, 502, , 17584, 100.00, Using index
EDIT:
And I forgot to mention what happens is that the databse just crashes with my query. i am using mysql-workbench
in Ubuntu 18
. When I perform this query the whole workbench closes and I have to restart opening it again.