Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed a SQL statement focus on combination of tables but entries always with unique ID
    primarykey
    data
    text
    <p>I need SQL code to solve the tables combination problem, described on below:</p> <p>Table old data: table old</p> <pre><code> name version status lastupdate ID A 0.1 on 6/8/2010 1 B 0.1 on 6/8/2010 2 C 0.1 on 6/8/2010 3 D 0.1 on 6/8/2010 4 E 0.1 on 6/8/2010 5 F 0.1 on 6/8/2010 6 G 0.1 on 6/8/2010 7 </code></pre> <p>Table new data: table new</p> <pre><code> name version status lastupdate ID A 0.1 on 6/18/2010 #B entry deleted C 0.3 on 6/18/2010 #version_updated C1 0.1 on 6/18/2010 #new_added D 0.1 on 6/18/2010 E 0.1 off 6/18/2010 #status_updated F 0.1 on 6/18/2010 G 0.1 on 6/18/2010 H 0.1 on 6/18/2010 #new_added H1 0.1 on 6/18/2010 #new_added </code></pre> <p>the difference of new data and old date:</p> <p>B entry deleted</p> <p>C entry version updated</p> <p>E entry status updated</p> <p>C1/H/H1 entry new added</p> <p>What I want is always keeping the ID - name mapping relationship in old data table no matter how data changed later, a.k.a the name always has an unique ID number bind with it. </p> <p>If entry has update, then update the data, if entry is new added, insert to the table then give a new assigned unique ID. If the entry was deleted, delete the entry and do not reuse that ID later.</p> <p>However, I can only use SQL with simple select or update statement then it may too hard for me to write such code, then I hope someone with expertise can give direction, no details needed on the different of SQL variant, a standard sql code as sample is enough.</p> <p>Thanks in advance!</p> <p>Rgs</p> <p>KC</p> <p>======== I listed my draft sql here, but not sure if it works, some one with expertise pls comment, thanks!</p> <h1>1.duplicate old table as tmp for store updates</h1> <p>create table tmp as select * from old </p> <h1>2.update into tmp where the "name" is same in old and new table</h1> <p>update tmp where name in (select name from new)</p> <h1>3.insert different "name" (old vs new) into tmp and assign new ID</h1> <p>insert into tmp (name version status lastupdate ID) set idvar = max(select max(id) from tmp) + 1 select * from (select new.name new.version new.status new.lastupdate new.ID from old, new where old.name &lt;> new.name)</p> <h1>4. delete the deleted entries from tmp table (such as B)</h1> <p>delete from tmp where (select ???)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload