MySQL - Add a column to Temporary Tables from another Table - mysql

I was wondering if you could add a column to a temporary table without the need to create a new one
Lets assume the following tables
Table1 | Table2 | Table3 |
Id Atype | Id Btype | Id Ctype |
1 A1 | 1 B1 | 1 C1 |
2 A2 | 2 B2 | 2 C2 |
3 A3 | 3 B3 | 3 C3 |
First i would like to create a temp table:
CREATE TEMPORARY TABLE IF NOT EXISTS
temp_table ( INDEX(id) )
AS (
SELECT t1.id, t1.atype , t2.btype
FROM table1 t1
left join table2 t2 on t1.id = t2.id);
Result:
temp_table
Id Atype Btype
1 A1 B1
2 A2 B2
3 A3 B3
Then i would like to add Ctype also in the temp table.
How can i do that?
Can i join in the current temp table or do i have to create a new temp table?
The end result i am looking is 1 temp table which looks like this:
Id Atype Btype Ctype
1 A1 B1 C1
2 A2 B2 C2
3 A3 B3 C3

Can't you just use another join?
SELECT t1.id, t1.atype, t2.btype, t3.ctype
FROM table1 t1 LEFT JOIN
table2 t2
ON t1.id = t2.id LEFT JOIN
table3 t3
ON t1.id = t3.id
If you actually want to modify the existing table, then:
alter table temp_table add ctype varchar(255);
update temp_table tt join
table3 t3
on tt.id = t3.id
set tt.ctype = t3.ctype;

You could do when creating addin a 3th join()
CREATE TEMPORARY TABLE IF NOT EXISTS
temp_table ( INDEX(id) )
AS (
SELECT t1.id, t1.atype , t2.btype, t3.Ctype
FROM table1 t1
left join table2 t2 on t1.id = t2.id
left join table3 t3 on t1.id = t3.id);

Related

select common values of multiple tables in mysql

I'm writing a php code to calculate a value. I have 4 tables with 2 column each. their headers are same. I want to fetch second column of tables that have, say 'MAN', value on their first column:
here are my tables and their dummy values:
I joined tables on 'dst' with a where clause but the problem arises when a table has does not have a common value and we have not full outer join in mysql. I know we can simulate this with union all operation but I want a efficient way to do this.
here is my try:
select t1.dst, t1.pay as pay, t2.pay as pay2, t3.pay as pay3, t4.pay as pay4 from t1 left outer join t2 on t1.dst = t2.dst left outer join t3 on t3.dst=t2.dst left outer join t4 on t1.dst=t4.dst where t1.dst='man';
it's dummy because left outer join is not true for this purpose except special cases.
actually I want this:
First union then pivot
drop table if exists t,t1,t2,t3;
create table t (dst varchar(3),value int);
create table t1 (dst varchar(3),value int);
create table t2 (dst varchar(3),value int);
create table t3 (dst varchar(3),value int);
insert into t values ('abc',10),('man',10);
insert into t1 values ('abc',10),('man',5);
insert into t2 values ('abc',10),('man',10);
insert into t3 values ('abc',10);
select dst,
MAX(CASE WHEN tbl = 't' then value end) as t1,
MAX(CASE WHEN tbl = 't1' then value end) as t1,
MAX(CASE WHEN tbl = 't2' then value end) as t2,
MAX(CASE WHEN tbl = 't3' then value end) as t3
from
(
select 't' as tbl,dst,value from t where dst = 'man'
union
select 't1' as tbl,dst,value from t1 where dst = 'man'
union
select 't2' as tbl,dst,value from t2 where dst = 'man'
union
select 't3' as tbl,dst,value from t3 where dst = 'man'
) s
group by s.dst;
+------+------+------+------+------+
| dst | t1 | t1 | t2 | t3 |
+------+------+------+------+------+
| man | 10 | 5 | 10 | NULL |
+------+------+------+------+------+
1 row in set (0.00 sec)

MySQL placement of conditions in on-clauses of multiple outer joins

As a follow up to In SQL / MySQL, what is the difference between "ON" and "WHERE" in a join statement? and SQL join: where clause vs. on clause - it does matter if a condition is placed in the on-clause vs. the where-clause in an outer join.
However, does it matter which on-clause the condition is placed in when there are multiple outer joins?
For example, could these produce different results?
select * from t1 left join t2 on t1.fid=t2.id and t2.col=val
left join t3 on t2.fid=t3.id;
vs:
select * from t1 left join t2 on t1.fid=t2.id
left join t3 on t2.fid=t3.id and t2.col=val;
Absolutely they are different.
The fisrt query will only have t2 rows that satisfy t2.col=val
The second query will include all t2 rows and only list t3 when t2.col=val
The queries are not equivalent. It is easy to construct a counter example:
create table t1 (id int not null, val int not null);
create table t2 (id int not null, val int not null);
create table t3 (id int not null, val int not null);
insert into t1 (id, val) values (1,1);
insert into t2 (id, val) values (1,1);
insert into t3 (id, val) values (1,1);
select * from t1
left join t2
on t1.id = t2.id
and t2.val = 2
left join t3
on t2.id = t3.id;
+----+-----+------+------+------+------+
| id | val | id | val | id | val |
+----+-----+------+------+------+------+
| 1 | 1 | NULL | NULL | NULL | NULL |
+----+-----+------+------+------+------+
select * from t1
left join t2
on t1.id = t2.id
left join t3
on t2.id = t3.id
and t2.val = 2;
+----+-----+------+------+------+------+
| id | val | id | val | id | val |
+----+-----+------+------+------+------+
| 1 | 1 | 1 | 1 | NULL | NULL |
+----+-----+------+------+------+------+
This is one of the queries:
select *
from t1 left join
t2
on t1.fid = t2.id left join
t3
on t2.fid = t3.id and t2.col = val;
Yes, the results are different. If you were using inner join they would be the same, but the left join changes things -- because the join clause does not do any filtering of rows.
I think the simplest explanation is that the join between t1 and t2 will include all rows from t1 as well as all matching rows from t2 -- even those where t2.col <> val. These remain in the result set, because the next left join does not filter them out.
In fact, the condition t2.col = val in the second on clause does not affect which rows are in the result set. If there is a match, then the row from t3 stays based on the first condition. If there is no match, then the row from t3 is still in the result set -- but the t3 columns will be NULL.
In this version:
select *
from t1 left join
t2
on t1.fid = t2.id and t2.col = val left join
t3
on t2.fid = t3.id;
The first join gets all rows from t1 and only matching rows from t2 where t2.col = val. The third join can then add more rows.
Note: there are definitely situations where the two queries would return the same results. But, the following data would generate different results (assume val = 0):
t1
fid
1
t2
fid col
1 0
1 1
t3
id
1
The query with the condition in the second on clause will return:
1 1 0 1
1 1 1 NULL
The query with the condition in the first on clause will return:
1 1 0 1

three tables with Inner Join

I have the following three tables...
Table1
IDA colB colC
111 a w
222 b w
333 c s
444 b g
Table2
IDB colB colC
11 w f
12 w r
13 s g
Table3
IDA IDB
111 11
222 12
333 13
444 14
What I need is to copy from table1 to table2 and I could use the following easy MySQL query to do that...
INSERT INTO table2 SELECT * FROM table1
The problem is I don't the same id type,...the two tables are connected over the third table table3.
in which IDA contains table1 primary key and IDB contain table2 primary key,
so, example if I want to copy from table1 IDA(111) to table2 how do I do that?
and if the IDB exists how do I update on Duplicate Key...
I have the following query but no working...
INSERT INTO table2 SELECT * FROM table1
WHERE IDA IN ( SELECT table1 b
INNER JOIN table3 c ON c.IDA = b.IDA
INNER JOIN table2 a ON a.IDB = c.IDB )
WHERE b.IDA=111
But, I wish if I get generalize answer...Thanks
INSERT INTO table2
SELECT
t3.idb
,t1.colb as ncolb
,t1.colc as ncolc
FROM
table1 t1
join table3 t3
on t1.ida = t3.ida
ON DUPLICATE KEY UPDATE
colb = ncolb
,colc = ncolc
No MySQL on me right now so syntax might not be 100% correct, but this should give you the idea of how it should be done.
Depending on whether table3 has entry for each table1 id you might need to change t3.idb to coalesce(t3.idb, t1.ida) and change join to left join in the query if you want them to be copied. Remember that table2 will then have ids from table1)
INSERT INTO table2 SELECT * FROM table1
WHERE IDA IN ( SELECT * FROM table1 b
INNER JOIN table3 c ON c.IDA = b.IDA
INNER JOIN table2 a ON a.IDB = c.IDB ) HAVING b.IDA=111

MySQL sum + inner join query

Question:
Table 1: id.1 | name.Joe | etc.Some | ...Other | ...Data
Table 2: id.X | number.+1 123 555 9999 | useridfromtable1.1 -> Linking telefone with Joe
Table 3: id. X | number.+1 123 555 9999 | calls.55
I need a query that join the 3 tables and I only have the id (userid) from the table 1.
So, I need from userid -> grab the telephone -> from the telefone grab calls on table3.
Answer:
SELECT t1.name,t1.id,t2.number,t3.calls
FROM table1 t1
INNER JOIN table2 t2 ON t2.useridfromtable=t1.id
INNER JOIN table3 t3 ON t3.number = t2.number
New Question?
Any chance you know how can I print the sum of calls on the result? after all these join I get about 10 lines of the same user and their respective calls based on the phone, what is correct for what I asked, now I need return all the calls in 1 line with the values:
sum | user ID | user Name
Maybe this will be useful:
SELECT SUM(t3.calls) FROM table1 t1 INNER JOIN table2 t2 ON t2.useridfromtable=t1.id INNER JOIN table3 t3 ON t3.number = t2.number
Try this query:
SELECT sum(t3.calls), t1.id, t1.name
FROM table1 t1
INNER JOIN table2 t2 ON t2.useridfromtable=t1.id
INNER JOIN table3 t3 ON t3.number = t2.number
GROUP BY t1.id

Query 2 MYSQL tables + condition?

Format -> column.example_data
Table 1: id.1 | name.Joe | etc.Some | ...Other | ...Data
Table 2: id.X | number.+1 123 555 9999 | useridfromtable1.1 -> Linking telefone with Joe
Table 3: id. X | number.+1 123 555 9999 | calls.55
I need a query that join the 3 tables and I only have the id (userid) from the table 1.
So, I need from userid -> grab the telephone -> from the telefone grab calls on table3.
TRY
SELECT t1.name,t1.id,t2.number,t3.calls
FROM table1 t1
INNER JOIN table2 t2 ON t2.useridfromtable=t1.id
INNER JOIN table3 t3 ON t3.number = t2.number
Try with something like:
SELECT t1.name,t1.id,t2.number,t3.calls
FROM table1 t1
INNER JOIN table2 t2 ON t2.t1_id = t1.id
INNER JOIN table3 t3 ON t3.t2_id = t2.id
Where t1_id and t2_id are the fields that are referring the records in the parent tables. I recommend you to add an index on those fields also.