dtnpf35197 2013-12-02 13:03
浏览 55
已采纳

如何将来自多个数据库的数据组合到一个结果集中。 (新旧系统)

I have an old and a new database with customer data. And now I have to remember if the customer I'm looking for is part of the new system, or the old system before i search - in my application. That's kind of a hassel.
I would like to join the two result-sets together and throw them into one single mega overview in my application - which is a table with a search function.

What would be the best practise for this?

Both the old and new database structure (system) consists of multiple tables joined together in order to get the necessary information needed. And they're not entirely equal.
The new system is splitted into more tables, and there are added a few new ones, to meet the needs when the business expanded their services.

But the result that is returned to the viewer is the same.

Would it be possible to just kind of merge the two arrays, that holds the result-set from the database query, together? - and then do a while-loop or foreach-loop on that?

Not really sure how yet, but if that's a solution I would appreciate some help looking in the right direction..

  • 写回答

2条回答 默认 最新

  • douao2000 2013-12-02 13:20
    关注

    Generally, you can combine results from multiple selects or tables using UNION or even UNION ALL on the database level. You just have to take care that your tables/selects return the same columns.

    For example:

    -- old table/database
    select t.customername as name, t.street as street, t.city as city 
    from old_data.customers t
    where t.name like 'M%'
    union 
    - new table/database
    select p.name as name, a.street as street, a.city as city
    from new_data.person p
    inner join new_data.address a on p.address_id = a.id
    where p.name like 'M%'
    

    With UNION it does not matter which names the tables or columns in the single SELECT statements have. What counts is that there are the same amount of columns and that they have compatible types.

    The actual problem may lie in accessing the tables from multiple databases. The solution for that depends on the DBMS you are using and where your old and new databases are located.

    If both databases are stored on the same server, as in your case, most DMBS (such as MySQL) will work for the query above (you prepend the database name to the table name as in database.table and you are good).

    If both databases were not stored on the same server, then the solution would depend on the DBMS. Oracle, for example, lets you specify database links, which you can use to span queries across multiple servers. Or, you can cache remote tables using materialized views.

    With MySQL, there are different possibilities, which are discussed in this question, e.g.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)