Join multiple databases mysql - mysql

I have roughly 2 Million records in two different databases, each has 1 million. I want to join the two tables with each other to find the differences but due to the data size the mysql throws a time-out error each time i perform the action. This is my query:
SELECT id FROM db1.table1 AS a INNER JOIN db2.table1 AS b ON ( b.Id != a.Id )
Any help would be appreciated !

You are joining each db1 record with all db2 records that have another ID. So thats about a trillion rows generated.
Try the following instead:
select * from db1.table1 where id not in (select id from db2.table1);
and
select * from db2.table1 where id not in (select id from db1.table1);
or combined:
select 'db1' as db, * from db1.table1 where id not in (select id from db2.table1)
union all
select 'db2' as db, * from db2.table1 where id not in (select id from db1.table1);

Try to use MySQL subquery:
SELECT id FROM db1.table1 WHERE id NOT IN (SELECT id FROM db2.table1);
The above query consists of two parts. The first part which is inside parenthesis selects field id from db2.table1 without any limitation. This result will be used in the outer one to remove unwanted records from db1.table1.

Related

Excluding column from MySQL SELECT distinct query with UNION across tables

I have an SQL database with several tables of patient data. Every table has one column in common, an ID number representing each patient. There is significant overlap between the tables, i.e. the same patient ID number often appears on multiple tables. What I would like to do is SELECT all distinct patient ID numbers that do not appear on one specific table.
You can use UNION and NOT IN like this:
select id
from (
select id from table1
union
select id from table2
union
select id from table3
...
) t where id not in (
select id from sometable
);

SQL Join 2 tables with almost same field

I need to join two tables in SQL. There are no common fields. But the one table have a field with the value krin1001 and I need it to be joined with the row in the other table where the value is 1001.
The idea behind the joining is i have multiple customers, but in the one table there customer id is 'krin1001' 'krin1002' and so on, in this table is how much they have sold. In the other table there customer is is '1001' '1002' and so on, and in this table is there name and adress and so on. So it will always be the first 4 charakters i need to strip from the field before matching and joining. It might not always be 'krin' i need it to work with 'khjo1001' also, and it still needs to join on the '1001' value from the other table.
Is that possible?
Hope you can help me.
You need to use substring:
ON SUBSTRING(TableA.Field, 5, 4) = TableB.Field
Or Right:
ON RIGHT(TableA.Field, 4) = TableB.Field
You can also try to use CHARINDEX function for join operation. If value from table1 contains value from table2 row will be included in result set.
;WITH table1 AS(
SELECT 'krin1001' AS val
UNION ALL
SELECT 'xxx'
UNION ALL
SELECT 'xyz123'
),
table2 AS(
SELECT '1001' AS val
UNION ALL
SELECT '12345'
UNION ALL
SELECT '123'
)
SELECT * FROM table1 AS t
JOIN table2 AS T2 ON CHARINDEX(T2.val, T.val) > 0
Use it as:
SELECT
*
FROM table t1
INNER JOIN table t2 ON RIGHT(t1.col1, 4) = t2.col1;

mysql request across 2 tables

I have one database with two tables:
table1: "backup1"
table2: "backup2"
both tables has a structure like
id, backupid, userid, backup, info
I would like to do a simple MYSQL request to get a group of entries where
it shows me what userid is found in table1 and ALSO in table2 to see what users did backups in both tables
I know I could open table1 look for a userid and do a another MYSQL to see if its also
in table2, but I hope there is a simpler way to do that with a single request.
SELECT userid
FROM backup1
INNER JOIN backup2
USING(userid)
The inner join on the userid field will cause the query to only return rows that are found in both tables.
This isn't through '2 databases', this is through '2 tables'. Just use a join.
SELECT a.userid
from backup1 a
inner join backup2 b
on a.userid=b.userid
This will give you all userids in backup1 that also exist in backup2
if you only need to know if a user made a backup in both tables, you could simply use simple select statements. A simple example, using a temporary variable #uId to hold the Id of the user you want to check, and two temporary variables to hold the (boolean) answer for each table
set #uId = 1; -- The user Id
select
#b_t1 := (select count(*) from table1 where userId=#uId) > 0 as hasBackupOnTable1,
#b_t2 := (select count(*) from table2 where userId=#uId) > 0 as hasBackupOnTable2,
#b_t1 and #b_t2 as hasBackupOnBothTables;

Insert non-duplicate rows from one table to another mysql

I have two tables T1 and T2. I want to merge only those rows of T2 which is currently not there in T1 .
`Insert into T1 select * from (select * from T2 where id not in (select id from T1))`
Is there a better and faste way of achieving the same. ID column is unique across the table
Insert into T1
select * from T2
where id not in (select id from T1)
You could also join but then you'd need another subselect since MySQL does not want to select from a table it inserts at the same time without using a subselect.

MySQL multiple SELECT statement count results returned per table

I'm trying to select 3 tables at once for a query. I would like to know what is the best way to determine how many rows are returned for each table?
Doing this individually, I have
SELECT * from tableA
SELECT * from tableB
SELECT * from tableC
If I do it this way, I can see on each select how many rows are returned. I would like to select all these at once which I've done successfully, but I'd like to know how to pick up results per table returned. Example query below:
SELECT * from tableA ta WHERE id=100
SELECT * from tableB tb where pid=100
SELECT * from tableC tc where cid = 100
Is it just a matter of doing this?
SELECT (count(id) from tableA where id=100) as count_tableA,
SELECT * from tableA ta WHERE id=100,
SELECT (count(pid) from tableB where id=100) as count_tableB,
SELECT * from tableB tb where pid=100,
SELECT (count(id) from tableB where cid=100) as count_tableC,
SELECT * from tableC tc where cid = 100
The overall goal is to increase performance by avoiding 3 queries each time, but with that, I need to isolate how many rows to pick up from each table that is returned.
Well, you can't really avoid the queries. You have to query each table to get the rows, and that requires a full-table scan.
You can optimize the counts by creating indexes on tableA(id), tableB(id), and tableC(cid).
You can also fetch the rows in your application layer, and then do the count afterwards.
The syntax for your query is not correct. Perhaps you mean:
select (SELECT count(id) from tableA where id=100) as count_tableA, a.*
from TableA
where id = 100;
And so on.