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;
Related
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!
This question already has answers here:
Advanced MySql Query: Update table with info from another table
(3 answers)
Closed 3 years ago.
I have two table let's name A and B having following field :
TABLE A
| ID | COUNTRY_CODE | COUNTRY_NAME | FIRST_NAME | LAST_NAME |
TABLE B
| ID | COUNTRY_CODE | COUNTRY_NAME |
Now I need to update country_code field from Table A whose value is to be fetched from Table B.
The pseudo-code is something like this :
for all rows in Table A :
set A.country_code = (select B.country_code from B where B.country_name = A.country_name
Use update with JOIN
update TableA A
inner join tableB B on B.country_name = A.country_name
set A.country_code=B.Country_code
No need join, just try sql as below:
update TableA set country_code = B.country_code from TableB B where A.country_name = B.country_name
I would like to create a relationship between two tables based on whether a value in one table falls in a an interval in the other. One table 1 is ~16000 rows:
name | start | end
-----------------------------------------
someName | startPosition | endPosition
table 2 is ~20000000 rows:
id | location
--------------------------
someID | positionInteger
Each id falls in the interval of exactly one name, but each name can have many ids associated with it.
I would like to add a new index to table 2 so that it becomes:
id | location | name
---------------------------------
someID | positionInteger | someName
I've tried doing:
ALTER TABLE table2 ADD INDEX name (name);
With a bit of python I can get all the names in the database and then for each name and interval:
SELECT someID FROM table2 WHERE location >= startPosition AND location <= endPosition
Then I can loop through the resulting IDs and:
UPDATE table2 SET name = 'someName' WHERE id = 'someID'
This works but is very slow. Is there a more efficient way to do this using MySQL and avoiding multiple loops?
Join the tables
SELECT t2.id, t2.location, t1.name
FROM Table1 AS t1
JOIN Table2 AS t2 ON t2.location BETWEEN t1.startPosition AND t1.endPosition
You shouldn't be adding an index to table 2, you should be adding a column:
ALTER TABLE Table2 ADD COLUMN name VARCHAR(32); -- Replace this with the actual size
Then you can update all the rows with a similar join:
UPDATE Table2 AS t2
JOIN Table1 AS t1 ON t2.location BETWEEN t1.startPosition AND t1.endPosition
SET t2.name = t1.name
You can use LEFT JOIN :
SELECT id,location,name FROM table2 as t2
LEFT JOIN table1 as t1
ON t2.location BETWEEN t1.`start` AND t1.`end`;
More details http://sqlfiddle.com/#!9/721e5/1
I have the following table structure.
create table t1 (
id int,
tno int
);
create table t2 (
id int,
detailno int
);
insert into t1 values (101,1);
insert into t1 values (101,2);
insert into t2 values (101,7);
insert into t2 values (101,8);
When I perform the following query:
select * from t1
inner join t2
on t1.id = t2.id
where t2.detailno = 8;
It performs a cross join and returns
id | tno | id | detailno
101 | 1 | 101 | 8
101 | 2 | 101 | 8
It is basically performing a Cross join instead of an inner join. Could you please help me return only a single result instead - since detail id = 8 is in the where clause ? I have shortened the table structure and the query for easier understanding. Here is the sql fiddle for the above code. http://sqlfiddle.com/#!9/92c98/1
Your query is doing exactly as you've asked it to do.
What is the one result you're expecting?
If you only want one line then maybe you're better off making the query more specific by adding a condition such as
WHERE t2.detailno = 8
AND t1.tno = 1
which will whittle down the results more.
Edit
If you're unsure what the value of t1.tno is going to be then could you not pass that in as a parameter? It might be more clear if you can explain why you're expecting t1.tno = 2.
You'd then potentially end up with something like this if you pass it in as a parameter.
WHERE t2.detailno = 8
AND t1.tno = #tno
Join is working as expected.
Execute this to check:
select * from t1
inner join t2
on t1.id = t2.id;
Every id is making join with another table's id and which is correct for join.
So if you want result as your expectation you need to pass t1.tno and add into where condition as there are multiple record in another table for same id (t1.id).
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