MySQL: joining tables through a series of relations - reducing duplicate table reads - mysql

I'm reading an article about join in MySQL, but I don't understand why Joining tables through a series of relations has the advantage of reducing duplicate table reads.
Can someone explain with examples? Thanks.
Quoted text below:
Stated visually, these two ways of joining tables looks like:
Series of Relations: table 1 -> 2 -> 3
Common Relation: table 1 -> 2, (1) -> 3
Either way, MySQL reads table 1, then 2, then 3—its "single-sweep
multi-join method." The "(1)" in the common relation join means that
when table 3 is read, rows in it are found using its relation to table
1, not table 2 as in the series of relations join. Joining tables
through a series of relations has the advantage of reducing duplicate
table reads if the first table is the most restrictive, the second
table less restrictive, the third table even less restrictive, etc.
Text from hackmysql.com/case5

I believe it is simply referencing to the following two (unrealistic) examples, although I could be wrong.
Note the way in which table 3 is joined in each.
Table 1 -> 2 -> 3
SELECT t1.name, t2.phoneNo, t3.address
FROM table1 t1
JOIN table2 t2
ON t2.id = t1.id
JOIN table3 t3
ON t3.id = t2.id
WHERE t1.id = 123
Table 1 -> 2, (1) -> 3
SELECT t1.name, t2.phoneNo, t3.address
FROM table1 t1
JOIN table2 t2
ON t2.id = t1.id
JOIN table3 t3
ON t3.id = t1.id
WHERE t1.id = 123
Regards,
Chris

Related

Single SQL statement based on the existence of foreign keys

I have two tables in a database
Table1
T1ID
T1Value
1
T1V1
2
T1V2
3
T1V3
4
T1V4
Table 2
T2ID
T1FK
T2Value
1
2
T2V1
2
3
T2V2
Please note that Table2 has a foreign key, that references T1ID in Table1. Also note that not all entries in Table1 have corresponding entries in Table2.
Now, I would like to use a SINGLE SQL statement to do both of the two following
If there are corresponding entries in Table2 related to Table1, then run the following statement
SELECT t1.*, t2.T2Value FROM Table1 AS t1 INNER JOIN Table2 AS t2 ON t1.T1ID = t2.T1FK WHERE t1.T1ID = 2
If there aren't any corresponding entries in Table2 related to Table1, then run the following statement (notice that NULL is returned instaed of t2.T2Value)
SELECT t1.*, NULL FROM Table1 AS t1 WHERE t1.T1ID = 4
(Please note that t1.T1ID is supplied by the program, so this value will change. I am just showing you what I need)
How do I combine both the above statements into a single statement?
Thanks.
A simple LEFT JOIN will do what you want. For example:
select t1.*, t2.t2value
from table1 t1
left join table2 t2 on t1.t1id = t2.t1fk
where t1.t1id = <parameter>
For example:
If the parameter is 2, then a related row is found and the query returns the value of it (t2.t2value).
If the parameter is 4, then no related row is found and the query returns NULL in its place.

Mysql multiple joins from 3 tables. clueless

I have 3 tables. i'm looking to connect each table but unsure how.
I'm looking to get all the information from table 1 and from the other tables only if it matches the fields in table 1
table 1 (id, name_1, name_2, name_3, info_1, info_2
table 2 (description,info_1,otherstuff_1)
table 3 (name_1,name_2,name_3,random_1)
I have tried doing a join but feel that I was using the wrong syntax.
if done correctly. I'm hoping to get
id,name_1.table3,info_1.table2,name_2.table3
any help is appreciated
Table names appear before column names.
Try something like:
select t1.id, t3.name_1, t2.info_1, t3.name_2
from table1 t1, table2 t2, table3 t3
where t1.info_1 = t2.info_1
and t1.name_1 = t3.name_1
You can join tables by this query
select t1.id,t3.name1,t2.info_1,t3.name_2 from table1 as t1 join table2 as t2 on t1.info_1 = t2.info_1 join table3 as t3 on t1.name_1 = t3.name_1 and
t1.name_2 = t3.name_2 and t1.name_3.t3.name_3

MYSQL want to compare two table and select data

I have two tables.
table 1
1
2
3
4
table 2
1
2
3
i want to select data from tables
table 1 have all data.
but table 2 may have all data or not.
so
if table 1 data exits in table two data select table 2 data otherwise select table one data.
if table 1 data not exit table 2 select table 1 data.
how can do that?
You need OUTER JOIN:
SELECT t1.ID,
COALESCE(t2.col1, t1.col1) AS col1, -- prefer data from table_2 if exists
COALESCE(t2.col2, t1.col2) AS col2,
-- ...
FROM table_1 t1 -- "table 1 have all data"
LEFT JOIN table_2 t2 -- "table 2 may have all data or not"
ON t1.ID = t2.ID;
Try FULL OUTER JOIN
SELECT t2.col1, t1.col1
FROM
table_1 t1 FULL OUTER JOIN table_2 t2
ON t2.col1 = t1.col1
Or, if full joins are not supported by your database, try emulating them.

Join two tables on two columns, even if table 2 does not have row

What I am trying to do is join two tables, lets call them t1 and t2 on two columns. id and name, for this example. t1 will always have id and name, but t2 won't always have id and name. t1 has more columns like viewes, reports, and t2 has other columns that need to be joined. My question is, how can I show 0's for t2's columns if they don't exist?
I hav something similar to this, that joins tables only if both tables' rows have some value.
SELECT
date(t1.start_time) date,
t1.name,
t1.viewes,
t1.reports,
t2.col5,
t2.col6
from
table1 t1
left outer join table2 t2
on t2.name = t1.name and date(t2.start_time) = date(t1.start_time)
group by
1,2
order by
1 desc,
2 asc
;
I have lot's of experience with MySQL, but sometimes find that things need to be hacked to work correctly. What's your suggestion for this problem?

Sql merging two text columns into single column

I have two tables t1 & t2.
table t1
user_id tags
1 a,b,c
2 b,c
table t2
user_id tags
1 d,c
2 c,d
I want to merge this into table t1. How can i do this.
table t1
user_id tags
1 a,b,c,d
2 b,c,d
I am new in sql.
Try this, although having more than one value in one field is not considered good practice and I couldn't recommend it.
SELECT t1.user_id, CONCAT(t1.tags, ', ', t2.tags) AS Tags
FROM table1 AS t1 INNER JOIN table2 AS t2 on t1.user_id = t2.user_id
This won't show unique values but will come close to what you request, which is hampered by your data format.
**
SQL Fiddle
**