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
Related
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)
I have two tables. t1 contains business info:
t1
id | busName | busPhone
t2 contains business hours
t2
id | busId | open | close
Where t1.id = t2.busId
I need to create a query and loop through it and if t2 does not have corresponding records, select those in order to add data. Clearly the query below is incorrect...
SELECT t1.id
FROM t1
LEFT JOIN t2 ON (t1.id = t2.busId)
WHERE t2.id != ''
This should be your query:
SELECT DISTINCT(t1.id)
t1 LEFT JOIN t2 ON (t1.id = t2.busId)
WHERE t2.busId IS NULL
select id from t1 where id not in (select busId from t2)
I have two tables as such.
t1
id name
1 foo
2 bar
t2
tid id address
1 1 foss
I can execute select satement to select id 1 from t1 from both tables.
select * from t1 inner join t2 on (t1.id = 1 and t2.id = 1)
But, what I actually want is select bar from t1, and only bar, even though it isn't present in t2.
The above tables are similar to what I have been working on my project, and resembles to the same problem.
Here is what I tried.
1. I used left join t1.id = 2 and t2.id = 2
It lists all the values from t1, which I only wanted t1.id = 2.
2.inner join could be used, but since t2.id = 2 is not present.
Therefore, it doesn’t return anything.
But I wanted something that would display data from t1 and t2 if id is present in both tables, and if not display only from table t1 of only that id.
So, if I wanted to display t1.id = 2, the answer I would expect is using
select * from t1 join .....
id name tid id address
2 bar null null null
Based on your expected result this is a simple Left Join:
select *
from t1 left join t2
on t1.id = t2.id
where t1.id = 2
Use left join to get all elements of t1 even if a element of t2 with same id not exists.
Then use null to get only t1 when t2 not exists.
At last, you can add a where filter to get a subset of t1 (ie: id = 2)
SELECT t1.*, t2.*
FROM t1
LEFT JOIN t2
ON t1.id = t2.id
WHERE t2.id IS NULL
AND t1.id = 2;
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
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.