简体   繁体   English

使用来自两个数据库的自动增量ID连接来自两个表的数据

[英]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.

相关问题 从C#中的两个数据库连接表 - Join tables from two databases in C# 从两个不同的表中搜索数据,这两个表在不同的数据库中 - Search data from two different tables that two tables in different databases 如何连接两个表并从中获取数据 - How to join two tables and fetch data from 如何在一个简单但完整的c#项目中从两个不同的sql server数据库连接两个表? - How to join two tables from two different sql server databases in a simple but complete c# project? C#-使用不同的ODBC驱动程序联接来自两个不同数据库的表 - C# - Join tables from two different databases using different ODBC drivers 使用join从两个表中获取数据并执行Count和GroupBy - Getting data from two tables with join and performing Count and GroupBy 将来自两个数据库的数据添加到一个gridview中 - Add data from two databases into one gridview 需要在来自不同服务器Postgresql数据库的两个表之间同步数据 - Need to sync data between two tables from different servers Postgresql databases 检查两个表中的ID,如果没有匹配项,将其删除 - Check id from two tables, if there is no match delete it 如何在实体框架中连接两个或多个表并在数据网格视图中显示每个表中的选定列 - How to join two or more tables in entity framework and display selected columns from each tables in data grid view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM