English 中文(简体)
can this be written with an outer join
原标题:

The requirement is to copy rows from Table B into Table A. Only rows with an id that doesn t already exist, need to be copied over:

INSERT INTO A(id, x, y)
SELECT id, x, y
FROM B b
WHERE b.id IS NOT IN (SELECT id FROM A WHERE x= t );
                                       ^^^^^^^^^^^

Now, I was trying to write this with an outer join to compare the explain paths, but I can t write this (efficiently at least).

Note that the sql highlighted with ^ s make this tricky.

最佳回答

try

INSERT INTO A(id, x, y)
SELECT id, x, y
FROM TableB b
  Left Join TableA a
     On a.Id = b.Id
        And a.x =  t 
Where a.Id Is Null

But I prefer the subquery representation as I think it more clearly expresses what you are doing.

问题回答

Why are you not happy with what you have? If you check your explain plan, I promise you it says that an anti-join is performed, if the optimizer thinks that is the most efficient way (which it most likely will).

For everyone who reads this: SQL is not what actually is executed. SQL is a way of telling the database what you want, not what to do. All decent databases will be able to treat NOT EXISTS and NOT IN as equal (when they are, ie. there are no null values) and perform an anti-join. The trick with an outer join and an IS NULL condition doesn t work on SQL Server, though (SQL Server is not clever enough to transform it to an antijoin).

Your query will perform better than the query with outer join.

I guess the following query will do the job:

INSERT INTO A(id, x, y)
SELECT id, x, y
FROM B b
LEFT JOIN A a
ON b.id = a.id AND NOT a.x= t 
INSERT INTO A (id, x, y)
SELECT
    B.id, B.x, B.y
FROM
    B
WHERE
    NOT EXISTS (SELECT * FROM A WHERE B.id = A.id AND A.x =  t )




相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签