How can I join a MS-SQL and MySQL table in a query?
I want to do a join accross a MS-SQL and MySql database.
The MS-SQL query generates an index, effectively, and then I want to pull all MySQL records back that match the result of this query. (I could bring back both tables, i.e. the unfiltered data from MySQL and then filter using Linq, but this will be inefficient as I'll be pulling back loads more data than I need.)
The MS-SQL query is done via Linq:
var fb1 = from f in db.tl_feedbacks
where f.timestamp >= new DateTime(fromYear, fromMonth, fromDay)
&& f.timestamp <= new DateTime(toYear, toMonth, toDay)
select new {f.requestID, f.tl_feedback_score.score };
This will bring back a table like this:
RequestID | score
-----------------
12345 | 1
12349 | 3
12446 | 3
etc.
From this I want to return only those records from the following MySQL query that have a RequestID in the above table:
SELECT wo.WORKORDERID,
COALESCE(ti.FIRST_NAME,'Not Assigned') AS 'Technician',
COALESCE(cd.CATEGORYNAME, 'Not Assigned') AS Category,
COALESCE(scd.NAME, 'Not Assigned') AS Subcategory,
wof.UDF_CHAR1 "Office Location"
FRO开发者_如何学PythonM WorkOrder_Threaded wot
INNER JOIN WorkOrder wo ON wot.WORKORDERID=wo.WORKORDERID
LEFT JOIN SDUser sdu ON wo.REQUESTERID=sdu.USERID
LEFT JOIN AaaUser aau ON sdu.USERID=aau.USER_ID
LEFT JOIN WorkOrderStates wos ON wo.WORKORDERID=wos.WORKORDERID
LEFT JOIN SDUser td ON wos.OWNERID=td.USERID
LEFT JOIN AaaUser ti ON td.USERID=ti.USER_ID
LEFT JOIN CategoryDefinition cd ON wos.CATEGORYID=cd.CATEGORYID
LEFT JOIN SubCategoryDefinition scd ON wos.SUBCATEGORYID=scd.SUBCATEGORYID
LEFT JOIN WorkOrder_Fields wof ON wo.WORKORDERID=wof.WORKORDERID
i.e I only want to pull back records 12345, 12349 and 12446 in this example. Ultimately I want one single table which has the requestID, score, and the columns from the MySQL query. However, if I can get the "filtered" MySQL table back I can join the two afterwards. I just don't want to bring the MySQL back "unfiltered" as the table will be huge.
With the right OLEDB database drivers (I've only done this with PGSQL so I can't really advise), you can create a Linked Server in MSSQL. Here's a walkthrough, and here's another.
You can then query it using OPENQUERY as follows in MSSQL:
select * from openquery(LinkedServerDb,'select * from remotetable')
and join:
select
*
from
openquery(LinkedServerDb,'select * from remotetable') remoteTable
join localTable on remotetable.someid=localtable.otherid
精彩评论