Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 8th, 2012  |  syntax: None  |  size: 0.74 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. SQLite3, Select From Two Tables One with Priority?
  2. ID Color Value
  3. 1 red    5
  4. 2 green  3
  5. 3 yellow 5
  6. 4 blue   1
  7. 5 white  4
  8.        
  9. ID Color Value
  10. 2 green  6
  11. 3 yellow 2
  12. 5 white  3
  13.        
  14. ID Color Value
  15. 1 red    5
  16. 2 green  6
  17. 3 yellow 2
  18. 4 blue   1
  19. 5 white  3
  20.        
  21. select t1.ID, case when t2.Color is not null
  22.                    then t2.Color
  23.                    else t1.Color
  24.               end as Color,
  25.               case when t2.Value is not null
  26.                    then t2.Value
  27.                    else t1.Value
  28.               end as Value
  29. from table1 t1
  30. left outer join table2 t2 on t1.id = t2.id
  31.        
  32. select t1.ID,
  33.        coalesce(t2.Color, t1.Color) as Color,
  34.        coalesce(t2.Value, t1.Value) as Value
  35. from table1 t1
  36. left outer join table2 t2 on t1.id = t2.id