[英]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). 一个归档了预定义的值(itemId,itemName),另一个则由用户填充(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. 属性itemId和itemName相同,从第一个表中选择所有数据,然后从第二个表中通过userId选择数据,并将这两个结果合并。
Joined data should be distinguish because ids are autoincrement and could be the same(doubled). 连接的数据应该有所区别,因为id是自动递增的,并且可以相同(加倍)。 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. 我们想用数据填充下拉列表,例如使用数据源和字典(int是itemId,string是itemName),我们在dropDownList中只能有valueFiled和valueText,但是这两个表的ID可以相同,而我们不希望这样做。无法从表中得知是由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 谢谢您的回答
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. 但是您必须撤消字符串连接以获得ID号。
select 'p:' || itemid, itemname
from predefined_values_table
union all
select 'u:' || itemid, itemname
from user_supplied_values_table
where userid = ?;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.