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
Related
I have two Mysql database dumps (DB1_10.10.2019 and DB2_10.02.2020). I created a custom query in DB1_10.10.2019 for getting the user details and made a 'report1'.
DB1_10.10.2019:
SELECT DISTINCT(...)
FROM ...
WHERE...
ORDER BY ...
For now, I have created a table called Table1 and stored the results.
INSERT INTO Table1(...,...,...)
SELECT DISTINCT(...)
FROM ...
WHERE...
ORDER BY...
Let's say after 4 months I want to get the user details, but this time I do not want users in report1. I want only new users. Can anyone please suggest a solution on how to achieve this?
I referred to the following site: https://www.mysqltutorial.org/mysql-minus/ and tried the following method:
SELECT
id
FROM
t1
LEFT JOIN
t2 USING (id)
WHERE
t2.id IS NULL;
The above query works only if we have two tables in the same DB. But in my case, I have two DB's.
MySQL version: 5.7.19
If the database is on the same machine and your user can access both, just enter the database name in the query as follows:
SELECT
`DB1_10.10.2019`.t1.id
FROM
`DB1_10.10.2019`.t1
LEFT JOIN
`DB2_10.02.2020`.t2 USING (id)
WHERE
`DB2_10.02.2020`.t2.id IS NULL;
Here you can find some examples
I have multiple suppliers of data, which I will call A, B, & C. A has a database that is updated monthly. B & C (my application actually gets more than 2 other data suppliers, and there are over 100) reference a table in A and tell which month from A they are using. A may update, add or delete records for each monthly release. Most of the records from A will stay the same. I currently use multiple databases, and specify the database to use in each join.
What is a good way to store the data from A so that B & C joins to the data will work efficiently? Does NoSQL or ORDBMS solve this issue?
If ...
you're using MySQL and
your user id has appropriate privileges and
your databases all live on a single server
you can use tables from multiple databases quite easily, just by qualifying the table names in your queries. For example, this sort of thing performs very well.
SELECT a.id, b.vendor
FROM A.stock a
JOIN B.shipments b ON a.sku = b.sku
I am migrating a news aggregation WP site to a commercial service. We currently have over 14,000 posts on it.
We want to save the current database and reuse it under a different domain name for historical purposes.
Once we move to the new site we want to trim the WP database of all posts and related tables that are older than 01.01.2013
I know how to do a simple select where delete query.
But WP forum mods have told me that I should do an inner-join on the following tables to ensure I get everything cleaned up:
wp_postmeta
wp_term_relationships
wp_comments
wp_commentmeta
I am not familiar with inner-join. Can someone help me with this?
Not completely understanding the table structures involved, an INNER JOIN will join one table to another and return the records that match based on a certain criteria (usually joining two fields together such as a primary key and a foreign key).
To delete records from one table where some or all are in another table, the following syntax would be used:
DELETE TableName
FROM TableName
INNER JOIN AnotherTable ON TableName.id = AnotherTable.id
Here's a good visual representation of JOINS.
Have have two tables in two different databases:
Lets say i have Database users, and Database questions. If users has a table called USER_STATS that has
USER_ID,
EDU_INT1,
EDU_INT2,
EDU_INT3
, and questions has a table called questions that have a column called CLASS_SUBJECTS.
I want to run a query that will display * from QUESTIONS where CLASS_SUBJECTS equals either EDU_INT1,EDU_INT2,EDU_INT3 where the EDU_INT's are determined from a specific USER_ID
Any ideas? This is semi hard because of the two different databases
When querying across two databases, you just need to prepend the database name with a . before the table name as in database.table.column, and the database connection user must have access to both databases.
Beyond that, this a regular JOIN, but with a more complex ON clause using 3 conditions OR'd together:
SELECT
q.*
FROM
questions.questions q
JOIN users.USER_STATS u ON (
q.CLASS_SUBJECTS = u.EDU_INT1
OR q.CLASS_SUBJECTS = u.EDU_INT2
OR q.CLASS_SUBJECTS = u.EDU_INT3
)
WHERE u.USER_ID = <some user id>
The database name reference might work if both are running in the same instance on a single server. If you need to scale to multiple servers, you might need to use replication and possibly set up federated tables.
http://dev.mysql.com/doc/refman/5.5/en/federated-storage-engine.html
I’m stumped on how to display a recordset via a mysql query that will show two different "real" usernames in a history table that has columns for multiple userIDs (ie createdbyUserID, and assignedtoUserID) – I can get one of them via a JOIN, but how do I JOIN etc to show both since they will likely be different username? Some other trick? Is it via sql or some other function/loop?
Currently:
SELECT nxt_act_dev_hist.Created, nxt_act_dev_hist.assignedtoUserID,
nxt_act_dev_hist.createdbyUserID, nxt_user.username
FROM nxt_act_dev_hist
JOIN nxt_user
ON nxt_act_dev_hist.createdbyUserID=nxt_user.UserID
I'm a newbie here if you can't tell.
You can join the same table twice, like this:
select
-- some other fields
createdByUser.UserName as CreatedByUserName,
assignedtoUser.UserName as AssignedToUserName
from
nxt_act_dev_hist
JOIN nxt_user as createdByUser
ON nxt_act_dev_hist.createdbyUserID = createdByUser.UserID
JOIN nxt_user as assignedtoUser
ON nxt_act_dev_hist.createdbyUserID = assignedtoUser.UserID
I'm not a mysql guy, but that should work.