update a field to other field in same table - mysql

I have a table named Users.
I import some users from other table.
they have a parent_id
my table i now
id,parent_id,imported_rows_id
1,1,NULL ->my old data has not last row value
2,1,Null ->my old data has not last row value
3,1,1100
4,1100,1101
5,1100,1102
6,1102,1103
Now i want to change all parent_id to id where imported_rows_id = parent_id
same as here:
3,1,1100
4,3,1101
5,3,1102
6,5,1103
update users set parent_id = (select id from users where parent_id=imported_rows_id)
Not allow on the same table
Sincerely

You can do it with a self join:
update TableName t1 join
TableName t2 on t1.imported_rows_id=t2.parent_id
set t2.parent_id=t1.id
where t2.imported_rows_id is not null
Result:
id parent_id imported_rows_id
--------------------------------
1 1 (null)
2 1 (null)
3 1 1100
4 3 1101
5 3 1102
6 5 1103
Result in SQL Fiddle

Related

Adding values in a column based on a query result

I have a table:---
id
name
dept
1
Alice
CS
2
Bob
Elect
3
David
Mech.
and a query result:-
id
count
1
100
2
22
3
50
Then I want to add the count column from the query to my original table, something like:-
id
name
dept
count
1
Alice
CSE
100
2
Bob
Elect
22
3
David
Mech.
50
The only I figured out to do, is by storing the query result into a new table and then using UPDATE...SET...WHERE. Is there any way to do it without creating a new table?
First you need to create the count column in tablename using
ALTER TABLE `tablename` ADD COLUMN `nr_count` INT;
Then use:
update tablename t
inner join ( SELECT id,
count(*) as nr_count
FROM tablename
GROUP BY id
) as t1
set t.nr_count=t1.nr_count ;

Mysql update row value in table based on different row

I have the following table structure
TABLE A
Productid price groupId
1 100 A
2 99 A
3 0 A
4 50 B
5 49 B
6 0 B
I populate table A with prices from table B joining on Id. Sometimes table B doesn't have prices.
In cases were b doesn't have price I want to update the price to be another price from that same group, as I can't have a price of zero.
Is there an way to update table a price column using itself based on group? e.g. update productId 3 price to be the price of another product in it's group (1 or 2)
TABLE A after update
Productid price groupId
1 100 A
2 99 A
3 100 A
4 50 B
5 49 B
6 49 B
It seems silly but these are the business rules (it makes sense irl I simplified the problem for the example)
When I tried the following I got Error:
update 'Table A' t1
join (select price ,groupId from 'table A' where Price > 0 group by
groupId) as t2
on t1.groupId = t2.GroupId
SET t1.Price = t2.Price
(conn=58292) Can't reopen table: 'Table A'
I've thought of creating a third temporary table but that seems.... wrong? I am sure there must be a way to do this using update statement and joins
I would phrase the query as:
update tablea a
inner join (select groupId, max(price) price from tablea group by groupId) a1
on a1.groupId = a.groupId
set a.price = a1.price
where a.price = 0 and a1.price > 0
Notes:
the table name should be surrounded with single quotes (those stand for literal strings) - if your table name really contains spaces, then use backticks for quoting (or better, yet, fix the table name!)
I changed the subquery to make it a valid aggregation query - yours has non-aggregated columns that do not belong to the group by clause, which is not a good practice, and might generate errors, depending on the SQL mode of your database
In this demo on DB Fiddlde with your sample data, the content of the table after update is:
Productid | price | groupId
--------: | ----: | :------
1 | 100 | A
2 | 99 | A
3 | 100 | A
4 | 50 | B
5 | 49 | B
6 | 50 | B

Select entries from table B with multiple conditions from table A

I've been trying to wrap my brain around this using joins, subquery joins, not exists clauses and I keep failing to come up with a query that produces the correct results.
Table A - PRIMARY id (irrelevant for this issue)
id | campaign_id | user_id
--------------------------
1 1 1
2 1 2
3 0 3
4 2 3
5 1 2
Table B - UNIQUE campaign_id+user_id
campaign_id | user_id | admin
-----------------------------
1 1 1
1 2 0
1 3 0
2 3 0
What I need to do is find instances of Table B where the user no longer has an entry in Table A that correspond with the campaign_id in Table B. Table A is the main content and they can have multiple entries of Table A that are present in the campaign. Table B is a member table that indicates they're a member of the campaign and whether they're an admin or not. In addition, they could have in entry in Table B as admin, but not have an entry in Table A, so the query must check for admin=0.
In the example entries, the invalid entry in Table B would be campaign_id 1, user_id 3
Use an outer join and then state in the where clause that the outer joined table's user_id is null:
select tblB.*
from tblB
left join tblA
on tblA.campaign_id = tblB.campaign_id
and tblA.user_id = tblB.user_id
where tblB.admin = 0
and tblA.user_id is null

Set with select clause fail

I have a table 1 that contains many entities and another one (table 2) that has 2 columns : the primary key of table 1 and a second column. An entity in table 1 has several other entities (from table 1 too) assigned by the second column in table 2
example:
id | type | ownerId
1 | bigObject | 1
2 | littleObj | 3
3 | littleObj | 3
id | key
1 | 2
1 | 3
and I want to update the ownerId of all littleObjects to the ownerId of the big objects.
What I tried:
UPDATE entity
SET ownerid = (SELECT ownerid
FROM entity
WHERE id = 1)
WHERE id IN (SELECT key
FROM table_b
WHERE id = 1)
;
But it says You can’t specify target table ‚entity‘ for update in FROM clause
You must be using MySQL, because that generates the error. Use this form instead:
UPDATE entity e CROSS JOIN
(SELECT ownerid
FROM entity
WHERE id = 1
) e1
SET e.ownerid = e1.ownerid
WHERE id IN (SELECT key
FROM table_b
WHERE id = 1)

Update last child id in parent table using mysql

Given the following tables:
Topic
id, last_updated_child_id
Response
id, topic_id, updated_at
How do I update the Topic table so the last_updated_child_id is equal to the latest response id (based on date).
So for example given:
Topic
id last_updated_child_id
-- -----------------------
1 null
2 null
3 null
Response
id topic_id updated_at
-- ---- ----
1 1 2010
2 1 2012
3 1 2011
4 2 2000
I would like to execute an UPDATE statement that would result in the Topic table being:
id last_updated_child_id
-- -----------------------
1 2
2 4
3 null
Note: I would like to avoid temp tables if possible and am happy for a MySQL specific solution.
Not very efficient, but relatively simple:
UPDATE topic
SET last_id = (SELECT id
FROM response
WHERE topic_id = topic.id
ORDER BY updated_at DESC
LIMIT 1);