tags:

views:

1993

answers:

9

Selecting the union:

select * from table1 
union 
select * from table1_backup

What is the query to select the intersection?

+3  A: 

In SQL Server intersect

select * from table1 
intersect
select * from table1_backup
Tom Ritter
Shoot, so that's just a SQL Server answer, is there no SQL Server/MySQL cross-db query?
Peter Turner
According to blog posts and listservs from 2005 I found on google, MySQL doesn't support the straight intersect notation, and instead suggests Inner Joins or subqueries.
Tom Ritter
The question doesn't state that a MySQL solution is required.Be aware that an Interest is also an implicit DISTINCT (same as UNION and MINUS)
David Aldridge
+1  A: 

inner join i think: suppose T1 and T2 have the same structure:

select T1.* from T1 inner join T2 on T1.pkField = T2.pkField

stefano m
The union is useful because it doesn't add a extra fields. It treats two tables as the same table when used. I want the same thing, except I want I to see which records are exactly the same in case something messed up in running a backup.
Peter Turner
+1  A: 

"intersect" is also part of standard SQL.

Inner join gives a different answer.

Walter Mitty
So what would the query be?select * from table1 intersect select * from table2 gives me an error in mysql
Peter Turner
A: 
select distinct * from (select * from table1 union select * from table1_backup)
James Curran
Excellent, this is the query I was looking for, stackoverflow is awesome, only took 5 minutes and was a lot more fun than Google-ing. This answer is perfect for me in every way except it needs to have "as table_x" at the end to not get a key violation in my version of SQL.
Peter Turner
This doesn't return an intersect... it returns every single row in each of the tables, and filters out duplicates.
Tom Ritter
Right, I just realized that. Unaccepted!
Peter Turner
+2  A: 
SELECT *
FROM table1
WHERE EXISTS
(SELECT *
FROM table1_backup
WHERE table1.pk = table1_backup.pk)

works

workmad3
This looks like a good answer, but not as abstract as the one I accepted. It's probably faster too.
Peter Turner
For my purposes, I didn't need the where clause.
Peter Turner
You need a WHERE clause otherwise it doesn't make sense. You'll just return all rows from table1 if any rows exist in table1_backup. Otherwise you'll get zero rows.
beach
A: 

For questions like this, I tend to go back to this visual resource:

A Visual Explanation of SQL Joins

Jeremy Wadhams
+1  A: 

here is a solution for mySQL:

CREATE TABLE table1(
id INT(10),
fk_id INT(10),
PRIMARY KEY (id, fk_id),
FOREIGN KEY table1(id) REFERENCES another_table(id),
FOREIGN KEY table1(fk_id) REFERENCES other_table(id)
);

SELECT table1.* FROM table1 as t0
INNER JOIN table1 as a ON (t0.id = a.id and fk_id=1)
INNER JOIN table1 as b ON (t0.id = b.id and fk_id=2)
INNER JOIN table1 as c ON (t0.id = c.id and fk_id=3)
ORDER BY table1.id;

Basically you have an table of mathematical subsets (ie. 1={1, 2 ,3}, 2={3, 4, 2}, ... , n={1, 4, 7}) with an attribute id, which is the set number, and fk_ id, which references a PRIMARY KEY of a table of elements, the superset (meaning possible values for the numbers in the curly braces). For those not mathematically inclined, let's pretend you have a table, 'other_ table', which is a list of items, and another table, 'another_ table', which is a list of transaction numbers, and both tables form a many-to-many relationship, thus producing 'table1'. now let's pretend you wanted to know the id's in 'another_ table' which had items 1, 2, and 3. that's the query to do it.

anonymous
A: 

subqueries?! really?

to get the intersection of table1 and table2:

SELECT * FROM table1, table2 WHERE table1.pk=table2.pk;
Stephen Wuebker
That's not the intersection, that's just a join. Unions work with two identical datasets, I was looking for a related function. You're right though, you could join on each field. The -1 is just for using the Lord's name in vain.
Peter Turner
A: 

An intersect on two identical tables a and b can be done in this manner:

SELECT a.id, a.name
FROM a INNER JOIN b
USING (id, name)
ovais.tariq