I'm looking for a way to update some values in a table if they were used in a left join.
I have two tables:
table1:
id | name | age | job
1 | john | 31 |
2 | eric | 25 |
table2:
id | job | inserted
1 | lawyer | 0
2 | dentist | 1
3 | cop | 0
Then I run the query:
UPDATE table1
LEFT JOIN
table2
ON table1.id = table2.id
SET table1.job = `table2.job`
WHERE table2.inserted = 0
But I want to update the rows from table2 that were used in the update so they have inserted = 1. This for two reasons 1) to speed up the join and 2) so I can check which rows of table2 were not used. (The inserts in table2 happen before table1, but the ids in table2 should always be present in table1 if all cron jobs run okay.)
You shouldn't be using a LEFT JOIN, since you only want to update rows in table1 that have a matching row in table2. Try:
UPDATE table1 AS t1
JOIN table2 AS t2 ON t1.id = t2.id
SET t1.job = t2.job, t2.inserted = 1
WHERE t2.inserted = 0
DEMO
Related
this is what I have, 2 tables, and i want to update table #2 with the stock from the first table, but i want to update only the first row of each duplicate.
+----+------+
|code|stock |
+----+------+
|5001| 40 |
|5002| 20 |
|5003| 60 |
+----+------+
+----+------+----+
|code|stock |lot |
+----+------+----+
|5001| | A | < Update this
|5001| | B |
|5002| | C | < Update this
|5003| | D | < Update this
|5003| | E |
+----+------+----+
So here i have the same product on two or more rows, and i want to update only the one from lot:A, lot:c, and lot: D, with the values from the first table.
How can i do this?
You could try using aupdate with join
update table2 t2
inner join (
select code, min(lot) lot
from table2
) t on t2.code = t.code and t2.lot = t.lot
inner join table1 t1 on t2.code = t1.code
set s2.stock = t1.stock
I am trying to delete records with the same id from 3 tables. The tables are something like this:
Table1
+----------+----------------+-----------+---------+----------+
| commonid | creation_date | column 1 | column 2| column 3 |
+----------+----------------+-----------+---------+----------+
Table2
+----------+---------+----------+---------+----------+
| commonid | column 1| column 2 | column 3| column 4 |
+----------+---------+----------+---------+----------+
Table3
+----------+---------+----------+---------+----------+
| commonid | column 1| column 2 | column 3| column 4 |
+----------+---------+----------+---------+----------+
So to select all the data I am using
SELECT * FROM table1
INNER JOIN table2
ON table1.commonid = table2.commonid
INNER JOIN table3
ON table1.commonid = table3.commonid
WHERE creation_date = '2018-08-01 04:13:50'
Which would return 6 rows. To delete I will try:
DELETE table1 FROM table1
INNER JOIN table2
ON table1.commonid = table2.commonid
INNER JOIN table3
ON table1.commonid = table3.commonid
WHERE creation_date = '2018-08-01 04:13:50'
Which returns 1 row affected, I would have expected 6. When I run the first query again I get 0 results. The total rows for table 3 are not affected.
How do I delete rows with the same commonid from each table?
You need to specify all three tables as targets for the deletion:
DELETE t1, t2, t3
FROM table1 t1
INNER JOIN table2 t2
ON t1.commonid = t2.commonid
INNER JOIN table3 t3
ON t1.commonid = t3.commonid
WHERE
creation_date = '2018-08-01 04:13:50';
Currently, you are only telling MySQL to delete from table1.
Is it possible in only one MySQL query to get the desired result (below) returned into one row ?
Where id=1 in Table 1
Table 1
|------|----------|----------|
| id | refIdOne | refIdTwo |
|------|----------|-----------
| 1 | 1 | 2 |
|------|----------|-----------
Columns "refIdOne" & "refIdTwo" refer Table 2 "id" column
Table 2
|------|------------------|
| id | text |
|------|------------------|
| 1 | cheese |
| 2 | made with milk |
|------|------------------|
Desired result returned in ONE ROW with custom AS columns named "subject" and "description" :
|----------|-----------------|
| subject | description |
|----------|-----------------|
| cheese | made with milk |
? Many thanks for help
* EDIT : Answer is *
select t21.text as subject, t22.text as description
from Table1 as t1
join Table2 as t21 on t1.refidone = t21.id
join Table2 as t22 on t1.refidtwo = t22.id
where t1.id = 1
Table2 has to joined twice to Table1.
select t21.text as subject, t22.text as description
from table1 t1
join table2 t21 on t1.refidone = t21.id
join table2 t22 on t1.refidtwo = t22.id
You need to perform Self join. Join Table2.ID twice with Table1.refIdOne and Table1.refIdTwo
select t2.text as subject, t3.text as description
from Table1 t1
Left join Table2 t2 on t1.refIdOne = t2.id
Left join Table2 t3 on t1.refIdTwo = t2.id
without joins
SELECT
(SELECT text FROM Table2 WHERE id = (SELECT refIdOne FROM Table1 WHERE id = 1) ) AS name,
(SELECT text FROM Table2 WHERE id = (SELECT refIdTwo FROM Table1 WHERE id = 1) ) AS description
I have two tables table1 and table2.
I'm doing some changes and I realized that table2 is not needed, but this table has lots of data already and I need to pass the values of ID_B from table2 to table1.
Here's the structure:
table1
ID_table1 | ID_table2 | ID_B
1 | 1 |
2 | 3 |
3 | 1 |
4 | 2 |
table2
ID_table2 | ID_B
1 | 14
2 | 26
3 | 26
So what I want is the MySQL query to pass the ID_B value from table2 to table1 when the ID_table2 on table1 is equal to the ID_table2 on table2.
For example, the row on table1 where the ID_table1 is 1 would have the ID_B = 14.
Can you help me on this?
Thanks in advance,
Miguel.
Using JOINs you can do as.
update table1 t1
inner join
table2 t2 on t2.ID_table2 = t1.ID_table2
set t1.ID_B = t2.ID_B
DEMO
You could try it like so:
UPDATE
table1 AS target,
(SELECT ID_table2, ID_B FROM table2) AS source
SET
target.ID_B = source.ID_B
WHERE
target.ID_TABLE2 = source.ID_table2
table1:
name | map_id | reg_id
abc | 1 | 5
pqr | 2 | 5
xyz | 3 | 5
table2:
map_id | map_name | is_deleted
1 | map1 | 0
2 | map2 | 0
my sql query :
SELECT *
FROM table1 t1
LEFT JOIN table2 t2
ON t1.map_id = t2.map_id
WHERE t1.reg_id = 5
AND t2.is_deleted = 0
what the above query does is stops me from retrieving record with map_id = 3 in table1.
how can i achieve this as well as 'is_deleted check' if the record exists in table2.
thanks in advance.
You need to move the is_deleted check to the JOIN:
select t1.name, t1.map_id, t1.reg_id, -- replace select * with columns
t2.map_name, t2.is_deleted
from table1 t1
left join table2 t2
on t1.map_id = t2.map_id
and t2.is_deleted = 0
WHERE t1.reg_id = 5;
See SQL Fiddle with Demo. Your current query is causing the LEFT JOIN to act like an INNER JOIN because you have the is_deleted as a part of the WHERE clause. If you move that to the JOIN, then you will return the rows from table2 that have is_deleted=0 and you will still return all rows from table1 where reg_id=5.