janilemy
janilemy

Reputation: 565

Join data from two tables with autoincrement id from two databases

I would like to join data from tables from two databases. One is filed with predefined values (itemId, itemName) and other is filled by user (itemId, itemName, userId). Atributes itemId and itemName are the same, from first table we select all data, from second we select data by userId and combine these two results.

Joined data should be distinguish because ids are autoincrement and could be the same(doubled). We would like to fill drop down list with this data for example with datasource and dictionary (int is itemId, string is itemName) we could only have valueFiled and valueText in dropDownList but ids of these two tables could be the same and lather we don`t know from wich table is item selected by id.

Is there any elegant way to joint this, maybe with temp tables that we always when user is logged in join these two tables in temp table etc...

Thank you for your answers

Upvotes: 2

Views: 391

Answers (1)

I think you're looking for a union. Use a literal value to identify the source.

select 'predefined' as source, itemid, itemname
  from predefined_values_table
union all
select 'user', itemid, itemname
  from user_supplied_values_table
  where userid = ?;

If you can accommodate only two columns, you might be able to use something like this instead. But you'll have to undo the string concatenation to get the id number.

select 'p:' || itemid, itemname
  from predefined_values_table
union all
select 'u:' || itemid, itemname
  from user_supplied_values_table
  where userid = ?;

Upvotes: 1

Related Questions