If there a way I can inner join a MS Sql table to a MySql Table in one query using MySql? - mysql

I have 2 servers one servers runs Microsoft SQL Server and the other one is using MySql.
I need to be able to inner join a table from MS SQL name it "A" to a table "B" located on a different server that uses MySql
So I want to be able to do something like this
SELECT A.*, B.* FROM A INNER JOIN B ON A.id=B.id LIMIT 100
How can I do this? note that both servers are on the same network.

1st link on google states...
you need to install this:
http://www.mysql.com/products/connector/
and follow this guide:
http://technikhil.wordpress.com/2007/05/13/getting-microsoft-sql-server-and-mysql-to-talk/
to link up the servers and then use openquery to execute MS SQL queries.

Related

Querying using multiple databases (connections) in dbeaver

I want to access multiple databases(multiple connections) at once in dbeaver and to the SQL querying.
As an example, I have 3 data connections: A, B and C.
I want to run a query like this:
select * from A
left join B on A.column=B.column
left join C on A.column=C.column
Is this not allowed in dbeaver?
No. You need a Data Virtualization or Data Integration tool.

connect to multiple ms access databases using odbc

When creating applications in MSAccess (VBA) you can connect to multiple databases (mdb files) by simply creating links to them. Now I have rewritten the user interface in C/C++ and using ODBC to connect to the database. How can I connect to a second database (mdb file) and joining data from tables from one database to the other. For instance database 1 (file1.mdb) contains a table invoice and database 2 (file2.mdb) contains a table prices. How can I join invoice with prices?
Assuming both databases reside on same network/server or machine, consider distributed queries where you qualify file names in brackets which is allowable with the Jet/ACE SQL engine.
SELECT p.*, i.*
FROM [C:\Path\To\File1.mdb].[Prices] p
INNER JOIN [C:\Path\To\File2.mdb].[Invoices] i
ON p.ID = i.PriceID
You can even connect to Excel workbooks, assuming data is in tabular format starting in A1 cell with column headers:
SELECT p.*, e.*
FROM [C:\Path\To\File1.mdb].[Prices] p
INNER JOIN [Excel 12.0 Xml;HDR=Yes;Database=C:\Path\Workbook.xlsx].[SheetName$] AS e
ON p.ID = e.PriceID
And same with CSV files:
SELECT p.*, c.*
FROM [C:\Path\To\File1.mdb].[Prices] p
INNER JOIN [text;database=C:\Path\To\CSV\Folder].CSVFile.csv AS c;
ON p.ID = c.PriceID

How to join two tables in different databases in Workbench

How does one join two tables that are in different databases using the SQL runner in MySQL Workbench?
I have searched for this and explored the interface but could not a find a solution.
If it is not possible with Workbench, is it possible with another client?
Note: the databases exist under different connections and ports!
You can simply join the table of different database. You need to specify the database name in your FROM clause. To make it shorter, add an ALIAS on it,
SELECT a.*, -- this will display all columns of dba.`UserName`
b.`Message`
FROM dba.`UserName` a -- or LEFT JOIN to show all rows whether it exists or not
INNER JOIN dbB.`PrivateMessage` b
ON a.`username` = b.`username`
So just adding DB name before tablename will solve your problem.
In that Case you can use,FEDERATED Storage Engine to join two mysql connections running on two servers.Please refer doc to know more about it
http://dev.mysql.com/doc/refman/5.0/en/federated-storage-engine.html

Need SQL aid for query with LEFT JOIN

I'm a beginner in sql and need some help using the left join (or alternate) function inthe following:
I have 2 tables:
1) Client
2) Server
Client has 2 columns (country and clientname) which is not present in Server. I want to join / copy these two columns into the Server table using the unique identifier column 'ClientID' present in both tables to match and join. How would I go about doing this and does anything recommend an easier way? I currently don't have physical access to a DB so I can't really test out any queries, so any help is appreciated!
Thank you
Are you looking for this?
SELECT s.*, c.country, c.clientname
FROM server s LEFT JOIN client c
ON s.clientid = c.clientid
Here is SQLFiddle demo.
For better understanding of JOINs see A Visual Explanation of SQL Joins

MS-Access DISTINCTROW dosen't work anymore if linked tables a stored on SQL2008-Server

I have a query in MS-Access like this:
select DISTINCTROW companies.* from companies, contacts, companies left join contacts on contacts.com_uid = companies.com_uid (This is the ms-access form of a standard "left-join")
[Companies] and [contacts] are linked views on a sql-server 2008, ODBC driver is "SQL server native client 10.0". Both views looks like "select * from [companies] where deleted = 0" and "select * from [contacts] where delete = 0"
The result is wrong since companies are show as many contacts there are.
If the Views are stored on a SQL2000 and linked with the ODBC-driver "SQL Server" everything is fine: All the companies are shown exactly once.
Are there any solutions to get the result with DISTINCTROW again?
I'm surprised it executes that query at all. You're specifying the table "contacts" twice.
Your LEFT JOIN should return every row from "companies". Since you're not retrieving any columns from contacts, I'm pretty sure your query is equivalent to
SELECT *
FROM companies
as long as "companies" means what it does in ordinary language.
If that turns out not to be the case, you can hand the burden off to SQL Server either by creating a view in SQL Server, or by creating a passthrough query in Access. A passthrough query will have to be written in your server's dialect of SQL (SQL Server 2008 dialect of SQL).
Your revision, reproduced below, does nothing to change my earlier comments.
select DISTINCTROW companies.*
from companies, contacts, companies
left join contacts on contacts.com_uid = companies.com_uid
(This is the ms-access form of a standard "left-join")
That's not Access's form of a left join. Access won't allow this:
from companies, contacts, companies
left join contacts
because you're now specifying both tables twice.
Based on your edit, I'd say the query you're trying to write is still equivalent to
SELECT *
FROM companies
What do you get if you run that?
Let's stop talking about the syntax of a left-join in ms-access. Fact is that if the linked tables are views on sql-server 2000:
create view [companies] as
select * from [TabCompanies] where deleted = 0
and
create view [contacts] as
select * from [TabContcts] where deleted = 0
These views are ODBC-linked-tables in a ms-access 2003/2007 mdb.
The questions shows up in ms-access on a query like
select distinctrow [companies].* from [companies] left join [contacts] on [companies].com_uid = contacts.com_uid] where [contacts].[function] like 'C*'
(lets forget that alternative syntax and look on the result assuming that the left join works without an error or syntaxerror)
This DISTINCTROW is a ms-access feature and not know in sql-server and for my point of view the result is the same like DISTINCT but works also even if there are columns with datatype of images par example.
All together we expect by now the same like Catcall in his answer said "select * from companies" BUT IT IS NOT, why?
This is only an excerpt of the whole query and may be makes no sense for production but it shows the changed behaviour wehn sql2008 is connected.
The purpose of DISTINCTROW is to make editable the two sides of an N:1 join. With a Cartesian product (from companies, contacts, companies), the result cannot be editable, so DISTINCTROW has no advantage over DISTINCT.
Secondly, no matter what you say, it is not possible to have the same table twice in a FROM clause without an alias. The SQL you've posted could not have worked in any version of Access.
The only way I can possible imagine there's any sense in what you've posted is if you've omitted a WHERE clause.
EDIT BASED ON COMMENTS:
This should work:
SELECT DISTINCT companies.*
FROM companies INNER JOIN contacts ON companies.com_uid = contacts.com_uid
WHERE contacts.function LIKE "C*"
First off, I'd assume a normal N:1 relationship between contacdts and companies (i.e., many contact records are linked to any single company record), so with both tables in the FROM clause, you do need a DISTINCT to return a single row for each company.
Secondly, if you place criteria on the table on the many side of the JOIN, there's no reason to attempt to use a LEFT JOIN, as it won't change the records returned (use a LEFT JOIN when you want to return records regardless of whether or not there are records in the table on the many side of the JOIN). So, an INNER JOIN is going to do the job for you, and be more efficient (outer JOINs are just slower, even with criteria).