I have data from two table. The column looks like this:
b_1,b_2,b_3,rule,name
And the other table columns are:
rule,name,a_1,a_2,a_3
And the resulting table that I would like is:
b_1,b_2,b_3,rule,name,a_1,a_2,a_3
Example data:
b_1,b_2,b_3,rule,name
ab,sd,ed,mine,abhi
gh,jk,ka,nice,sid
rule,name,a_1,a_2,a_3
mine,abhi,qw,er,rt
nice,sid,zx,zx,cv
And the resulting data that I need is:
b_1,b_2,b_3,rule,name,a_1,a_2,a_3
ab,sd,ed,mine,abhi,qw,er,rt
gh,jk,ka,nice,sid,zx,zx,cv
I have tried this queries:
Select b_1,b_2,b_3,b.rule,b.name,a_1,a_2,a_3 from T1 a INNER JOIN T2 b ON b.rule=a.rule and b.name=a.name;
Select b_1,b_2,b_3,b.rule,b.name,a_1,a_2,a_3 from T1 a LEFT JOIN T2 b ON b.rule=a.rule and b.name=a.name;
P.S:
Table T1 has 31465 rows.
Table T2 has 31465 rows.
So even Table T3 should also contain 31465 rows.
Both tables have redundant rows.
Any help would be appreciated.
there are two conditions
1) if the two tables have relation of N-M(meaning that each item of table1 has M items in table2 and each item in table2 has N items in table1)
2) if the two tables have relation of 1-M(meaning that each item of table1 has M items in table2 and each item in table2 has 1 item in table1)
in case 1 you have to make another table using the combining columns of both table which in your case it's rule,name and for example lets call it jointable , then the tables would be like
table1(a1,a2,a3,rule,name)==>primary key(rule,name)
jointable(rule,name) ==> primary key(rule,name)
table2(b1,b2,b3,rule,name) ==>primary key(rule,name)
and the select statement would be
select table1.a1 ,table1.a2 ,table1.a3,table1.name,table1.rule,table2.b1,table2.b2,table2.b3
from jointable
join table1 on table1.rule = jointable.rule and table1.name = jointable.name
join table2 on table2.rule = jointable.rule and table2.name = jointable.name
and
in case 2 one table contains primary key(like rule and name in table1) and the other table (like table2) contains foreign key referencing the parent table primary key which in this case would be rule,name of table1(note that table 1 is the parent and the child which is table 2 cannot contain a value that the parent doesn't have)
so tables would be like:
table1(a1,a2,a3,rule,name) ==>primary key(rule,name)
table2(b1,b2,b3,rule,name) ==>foreign key(rule,name) references table1(rule,name)
and the select statement would be like:
select table1.a1 ,table1.a2 ,table1.a3,table1.name,table1.rule,table2.b1,table2.b2,table2.b3
from table1 join table2 on table1.name=table2.name and table1.rule=table2.rule
The answer that worked for me was that I had to add a unique identifier to each row on both the tables. The column rule and name were common to both the tables but both had redundant entries and I wanted to preserve the redundant data so I updated my tables with new id column.
T1:
id,b_1,b_2,b_3,rule,name
T2:
id,rule,name,a_1,a_2,a_3
The id column is NOT NULL and auto incremented.
Now I had a common unique column through which I can join both the table.
Select b.b_1, b.b_2, b.b_3, rule, name, a.a_1, a.a_2, a.a_3 from T1 b INNER JOIN T2 a ON b.id=a.id;
This query gave me the desired result.
I have 2 tables:
Table1- contains PhoneNumber|Name
Table2- contains PhoneNumber|Address
I want to create Table3 with PhoneNumber|Name|Address
Table1 and Table2 may have out of order or different amount of entries, therefore table1 will act as main list.
please suggest way forward. using MySQL.
Use CREATE..SELECT with a LEFT JOIN :
CREATE TABLE Table3 AS
SELECT t1.PhoneNumber,t1.name,t2.address
FROM Table1 t1
LEFT JOIN Table2 t2
ON(t1.PhoneNumber = t2.PhoneNumber)
I have 2 tables. I want to find out whether the values present in the first table is there in another table with a different field name.
Here is how it looks,
Table1
BillNo
43529179
43256787
35425676
25467778
24354758
45754748
Table2
BNo
113104808
25426577
268579679
2542135464
252525232
235263663
I have 137 records in table1 that needs to be checked against table2.
Instead of doing it one by one using the following command,
Select * from Table2 where BNo = '43529179';
This gives the result for just the mentioned value. Is there a way to check for all the values in a single query?
Thanks!
You can use a sub-select to compare against:
Select * from Table2 where BNo IN (SELECT BillNo FROM Table1);
That will "evalaute" to something like Select * from Table2 where BNo IN (113104808, 25426577, 268579679, 2542135464, 252525232, ...);
Join the tables, and check how many matching records there are:
select
count(*) as Matches
from
Table1 as t1
inner join Table2 as t2 on t2.BNo = t1.BillNo
You can also use a left join to pick out the records in table 1 that has no matching record in table 2:
select
t1.BillNo
from
Table1 as t1
left join Table2 as t2 on t2.BNo = t1.BillNo
where
t2.BNo is null
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.
i got two tables like
table1
=======
id primary
sku
price
vendor
table2
=====
id primary
sku
alt_sku
basically, my search term will be sku or alt_sku, and based on it, I want to select data from table1
what will be best approach to do this? should I create a third table to store relation between table1.sku and table2.sku /table2.alt_sku?? or how can I get data from table1
sorry, if this question is veryy foolish...I am trying to learn mysql, so for, I was on WP and never went to basics
You can try an inner join. Assuming TERM is the sku you are searching for.
SELECT
t1.*
FROM
table1 t1
inner join table2 t2 on (t1.sku = t2.sku)
WHERE
t1.sku = 'TERM'
or t2.alt_sku = 'TERM'
What exactly are you trying to do with Table2? Perhaps a better approach would be the following:
Table1
id
price
vendor
Table2
Table1Id
Sku
That way you can store a 1-N number of SKUs associated with Table1. If you only want 2 potential SKUs, then you could add a SkuType field to your Table2 - 1 for Primary and 2 for Alternate, but that may be going too deep for your needs...
And to query:
SELECT T.*
FROM Table1 T
JOIN Table2 T2 ON T.Id = T2.Table1Id
WHERE T2.Sku = 'SomeSku'
I think your table structure should be like that:
table1
=======
id primary
IdFromtable2
price
vendor
table2
=====
id primary
sku
alt_sku
Your Select Query will be like:
SELECT t1.*
FROM table1 t1
INNER JOIN table2 t2 ON t1.IdFromtable2 = t2.Id
WHERE t2.sku = #sku AND
t2.alt_sku = #alt_sku
Basically,The finger rule for table relationships is as following:
If the relations is one-to-many then you should save the PK from the many table as additional column in the one table.
If the relations is many-to-many you should create a third table (connecting table). This table will have both PK from both tables (for each tbl1 PK you will have all tbl2 PK's and vice versa).
That way you preventing duplicated records.