in

System.Data.SQLite

An open source ADO.NET provider for the SQLite database engine

Inner Join in Update

Last post 05-02-2009 5:04 AM by freshj. 4 replies.
Page 1 of 1 (5 items)
Sort Posts: Previous Next
  • 04-26-2009 5:27 AM

    Inner Join in Update

    Is there a way to do an update of one table another?  For example

    UPDATE TABLE_A
    SET name = TABLE_B.name
    INNER JOIN TABLE_B
    ON TABLE_A.id = TABLE_B.id

     

    Thanks in advance

     

     

     

  • 04-28-2009 8:50 PM In reply to

    Re: Inner Join in Update

    I went through this recently in my SQLite app, and came to the conclusion that you have to do it as a sub-select, e.g.:

    update table_a set name = (select table_b.name from table_b where table_b.id = table_a.id)

    SQLite seems to process this fairly efficiently as far as I can tell.

  • 05-01-2009 3:40 AM In reply to

    Re: Inner Join in Update

    Thank you.  I tried this but that table is really big and it was not performing well even if it does work.  I had indexes in place as well.

  • 05-01-2009 6:58 AM In reply to

    Re: Inner Join in Update

    Is the update / subselect query as simple as you show in the example, or is it more complex?  If it's complex, it might improve things at least to the point of usability to create a temporary table, e.g.:

    create temp table xyzzy as select foo as id, bar as name from <complex query>;
    update table_a set name = (select name from xyzzy where table_a.id = xyzzy.id);
    drop table xyzzy;

    Alternately, it looks like with some proper trickery, insert or replace may be able to do what you want: http://www.mail-archive.com/sqlite-users@sqlite.org/msg27207.html.  Consider this example:

    create table foo (id integer primary key, name text not null, extra text not null);
    create table bar (id integer primary key, name text not null);
    insert into foo values (1, "one", "first");
    insert into foo values (2, "twox", "second");
    insert into bar values (2, "two");

    insert or replace into foo (id, name, extra)
    select bar.id, bar.name, foo.extra
    from bar left join foo on bar.id = foo.id;

     

  • 05-02-2009 5:04 AM In reply to

    Re: Inner Join in Update

    I saw a post by Richard Hipp about doing this (INSERT OR REPLACE) as well.  My experience with INSERT OR REPLACE is that the primary keys are not preserved.  I will have to try it out again.  Thanks

Page 1 of 1 (5 items)
Powered by Community Server (Commercial Edition), by Telligent Systems