Query 2 MYSQL tables + condition? - mysql

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.

Related

Join query results in MySQL

I am working in a MySQL database.
I have three tables. 1 , 2 & 3
I would like to join the result of joining tables 2 & 3 to table 1 on an id. I would like to keep all entries for table 1 and after join the result of 2 & 3 on the call id and where it doesn't match have a null value.
Table 1 has callid
Table 2 has callid and invoiceid
Table 3 has invoiceid and customerid
So join table 2 & 3 on invoiceid and filter by customerid = xyz the result of that is then joined to table 1 on callid. Table 1 would also have a Where clause filtering the on a date
result would look like this
callid customerid
123 xyz
124 xyz
125 null
126 xyz
thanking you in advance
I think you are describing left join:
select . . .
from table1 t1 left join
table2 t2
on t1.callid = t2.callid left join
table3 t3
on t3.invoiceid = t2.invoiceid;
You can filter on customer id in the on clause:
from table1 t1 left join
table2 t2
on t1.callid = t2.callid left join
table3 t3
on t3.invoiceid = t2.invoiceid and t3.customerid = ?;
However, this might not be exactly what you want. You might want to filter for the inner join and then do the left join:
select . . .
from table1 t1 left join
(table2 t2 join
table3 t3
on t3.invoiceid = t2.invoiceid
)
on t1.callid = t2.callid and
t3.customerid = ?

MySQL: Join three tables, comparing two values

First of all, I'm an amateur on SQL. Here it is the example. From this three tables I would like to know who are the teachers that make more money than Mike
Table1:
LessonName TeacherID
Maths 3
Biology 2
Biology 4
Geology 1
Table2:
Lesson PricePerClass
Maths 200
Biology 100
Geology 150
Table3:
IDTeacher TeacherName
1 Mike
2 John
3 Lauren
4 Julian
So far I've made this:
select t3.IDTeacher, sum(t2.PricePerClass) TotalRevenue
from Table3 t3
inner join Table1 as t1 on t1.TeacherId = t3.IDTeacher
inner join Table2 as t2 on t2.Lesson = t1.LessonName
group by t3.IDTeacher
where TotalRevenue > (select TotalRevenue
from Table2 as t2
inner join Table1 as t1 on t2.Lesson = t3.LessonName
inner join Table3 as t3 on t3.IDTeacher = t1.TeacherID
where t3.TeacherName = "Mike");
And I don't know how to keep going because when I run it, an error appears.
My expected result would be something like:
IDTeacher TotalRevenue
3 200
Thanks!
In your main query you must use a HAVING clause instead of a WHERE clause and also in the subquery fix your joins:
select t3.IDTeacher, sum(t2.PricePerClass) TotalRevenue
from Table3 t3
inner join Table1 as t1 on t1.TeacherId = t3.IDTeacher
inner join Table2 as t2 on t2.Lesson = t1.LessonName
group by t3.IDTeacher
having TotalRevenue > (
select sum(t2.PricePerClass)
from Table3 t3
inner join Table1 as t1 on t1.TeacherId = t3.IDTeacher
inner join Table2 as t2 on t2.Lesson = t1.LessonName
where t3.TeacherName = "Mike"
);
See the demo.
Results:
| IDTeacher | TotalRevenue |
| --------- | ------------ |
| 3 | 200 |

MySQL - Add a column to Temporary Tables from another Table

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);

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

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