This question already has answers here:
SQL left join vs multiple tables on FROM line?
(12 answers)
Closed 2 years ago.
I would like to know what is the best way to create a connection between two tables (or more).
I have seen two different ways to do so.
In the next example, I have 2 tables. Table1 has customerId (key) and customerName. Table2 has customerId (key) and CustomerPhone
table1:
customerId | customerName|
============+=============+
1 | Josh |
2 | Nadia |
table2:
customerId | customerPhone|
============+==============+
1 | 123 |
2 | 456 |
Which query is the best and why:
SELECT Table1.customerId, Table2.customerPhone
FROM Table1, Table2
WHERE Table1.customerId = Table2.customerId
Query2:
SELECT Table1.customerId, Table2.customerPhone
FROM Table1
Inner Join Table2 ON Table1.customerId = Table2.customerId
The second option is more common and considered to be the right one, however they are both doing the same thing under the hood
The first query makes first a cross join and then reduces the result to fit the where cflause.
The second matches the tables by check ing if the condition in the ON clause ist met.
Both do the same, but the first is slowe
The first query is written using old syntax (SQL-89):
SELECT Table1.customerId, Table2.customerPhone
FROM Table1, Table2
WHERE Table1.customerId = Table2.customerId
The second query is written in modern syntax (SQL-92):
SELECT Table1.customerId, Table2.customerPhone
FROM Table1
Inner Join Table2 ON Table1.customerId = Table2.customerId
They are equivalent. Use SQL-92. Use SQL-92!
Related
I would like to update table 1 column 'num' with the value in table2.
might be really basic.. but I could not find the difference between using
inner join and update table x, y where xxxx in this scenario.
let's say I have two tables:
table1
name | num
name1 1
name2 2
name3 3
table2
name | num
name1 11
name2 22
name4 44
what I want is:
table1:
name | num
name1 11
name2 22
name3 3
I could achieve this by running
Method1:
UPDATE table1, table2
SET table1.num = table2.num
where table1.name = table2.name
and
Method2:
UPDATE table1
inner join table2 on table1.name = table2.name
SET table1.num = table2.num
I get the same result and I was wondering if I can use either one.. or is there any difference between these two queries??
I think I need use method1 to update both table..
but if I want to update only 1 table like in this scenario, does it matter which query I use?
The two are functionally equivalent -- and both are non-standard SQL syntax (i.e. MySQL extensions). They should produce the same execution plan as well.
That said, I recommend the version with the JOIN. In my opinion, JOIN is always preferable over commas.
This question already has answers here:
What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN? [duplicate]
(3 answers)
Closed 6 years ago.
I have two tables in my database
--------- ---------
| A | | B |
|---------| |---------|
| id | | id |
| name | | a_id |
--------- | name |
---------
I want to select all the records in table A only if there is a record in table B that points to the record in table A ( B.a_id = A.id).
How can i achieve this? And I'd like an explanation so I understand how it's done.
Use an INNER JOIN:
SELECT DISTINCT A.*
FROM A
INNER JOIN B ON B.a_id = A.id
Because of the INNER JOIN, for each row in table A, MySQL finds all the rows from B that match the JOIN condition (B.a_id = A.id).
The SELECT clause instruct it to return only the columns from table A (A.*) but because of the JOIN, a row in A can match more than one row in B. The DISTINCT clause takes care to avoid having the same output row multiple times in the result set.
SELECT *
FROM a
WHERE id IN (SELECT a_id from b)
This is a basic INNER JOIN
SELECT a.*
FROM a
INNER JOIN b ON a.ID = b.a_ID
Inner joins will select all the records from the specified table(s) where there is a match on the join criteria (the ON operator).
Sorry for bad title, but I don't know what is should be.
I want to join two tables but the number of line should be same as table one.
Here are my tables
====Table 1=====
| Name | IdCardNo |
===============
| Peter | 1234 |
| Mary | 5678 |
===============
==========Table 2=========
| IdCardNo | phoneNo | Job |
=========================
|1234|11111111|Student|
|1234|11111111|Waiter|
|5678|22222222|Student|
=========================
Here is the result i want
Peter|1234|11111111|
Mary|5678|22222222|
However if i do SELECT NAME, PhoneNo FROM Table1 LEFT JOIN Table2 on Table1.IdCardNo=Table2.IdCardNo
I get this
Peter|1234|11111111|
Peter|1234|11111111|
Mary|5678|2222222|
I know I can do GROUP BY NAME but I don't think it is a good idea.
What query should I use?
I think SELECT DISTINCT... is the thing I want. Thank you!
Just use DISTINCT
The SELECT DISTINCT statement is used to return only distinct
(different) values.
Something like
SELECT DISTINCT NAME, PhoneNo
FROM Table1 LEFT JOIN
Table2 on Table1.IdCardNo=Table2.IdCardNo
Maybe you might want to reconsider the design of the tables. Unless a different (possible) number is required per instance of table2, you might want to store the number in table1?
Your expected result is ambiguous
Mary|5678|11111111|
I suppose should be
Mary|5678|22222222|
This could be achieved as
select
t1.Name,
t1.IdCardNo,
t2.phoneNo
from table1 t1
join table2 t2 on t1.IdCardNo = t2.IdCardNo
group by t1.Name,t1.IdCardNo,t2.phoneNo
SELECT distinct t1.NAME, t1.idCardNo, t2.PhoneNo FROM Table1 t1 LEFT JOIN Table2 t2 on t1.IdCardNo=t2.IdCardNo
Distinct will skip identical rows in the result set!
'Select distinct' will only populate the joined table with unique values. Try that and let me know.
This question already has answers here:
MySQL UPDATE syntax with multiple tables using WHERE clause
(4 answers)
Closed 8 years ago.
Since I can't find any query here that works for me I've decided to ask a question.
I have table1 which has the next columns:
id | name | address | other_id
-------------------------------
1 | john | blvd 123| null
I have table2 which has the next columns:
id | other_id
--------------
1 | 20301
I would like to update table1.other_id with the table2.other_id according to the id.
What's the syntax for it ?
Thanks.
try this:
UPDATE table1 JOIN table2 ON table1.id = table2.id set table1.other_id=table2.other_id
UPDATE table1 JOIN table2
ON tabel1.id = table2.id
SET table1.other_id=table2.other_id;
Use this query
UPDATE table1 u
INNER JOIN table2 s on
u.other_id= s.other_id
SET u.other_id= s.other_id
Firstly, table1 should point table1.other_id as foreign key mapped to table2.other_id. For insert you can use the following statement:
INSERT INTO table1(other_id) SELECT table2.other_id FROM table2 WHERE table2.other_id=1;
This question already has answers here:
Alternative to Intersect in MySQL
(9 answers)
Closed 9 years ago.
Results 1
Zac
Dave
Ned
Results 2
Eric
Mark
Zac
This is the output from a select query.
select names from table where id=1 UNION select names from tables where id=2;
I want to select all from these results that are contained in both results. Union returns all the names (Zac only once). How do I get the query to only return Zac?
This should do it:
SELECT name FROM table1
INNER JOIN table2
USING (name)
Result
| NAME |
--------
| Zac |
See the demo
Just use an INNER JOIN between the two tables.
SELECT a.name
FROM table1 AS a
JOIN table2 AS b ON a.name = b.name