I have:
Table_A with this fields: ID (primary key, unique), Name, Attribute_A
Table_B with this fields: ID (number), Name, Attribute_B
I would like that Table_B.Name filled in this way:
UPDATE Table_B
SET Table_B.Name = Table_A.Name
WHERE Table_B.ID = Table_A.ID
how can I do that?
You can use an INNER JOIN in an UPDATE query:
UPDATE Table_B
INNER JOIN Table_A ON Table_B.ID = Table_A.ID
SET Table_B.Name = Table_A.Name
Note that both tables are required to be updateable, not only the one being updated.
Related
Our database didn't implement ON DELETE CASCADE so I need to create a script or a stored procedure to delete records in multiple tables given the results of Ids in a QUery:
I tried it in stored procedure but I cannot assign multiple rows in a variable:
CREATE PROCEDURE `Delete_card`(IN _status INT)
BEGIN
SET #IDs = SELECT ID FROM TABLE_ID WHERE Status=_status;
DELETE FROM TABLE_A WHERE ID IN (#IDs);
DELETE FROM TABLE_B WHERE ID IN (#IDs);
DELETE FROM TABLE_C WHERE ID IN (#IDs);
DELETE FROM TABLE_D WHERE ID IN (#IDs);
DELETE FROM TABLE_E WHERE ID IN (#IDs);
END
You can do it with a multi-table DELETE statement:
DELETE a, b, c
FROM TABLE_ID t
LEFT JOIN TABLE_A a ON a.id = t.id
LEFT JOIN TABLE_B b ON b.id = t.id
LEFT JOIN TABLE_C c ON c.id = t.id
..................................
WHERE t.Status = ?;
Change ? with the Status value that you want.
See a simplified demo.
table_a contains all orders, while table_b contains only special orders. Every order in each table has a code_field. All orders in table_b are also in table_a, but of course not all orders in table_a are also in table_b. I need to extract all orders in table_a that are not also in table_b. Looking for a solution but I actually cannot figure out how to write it.
You can select everything from table_a and left join table_b by code_field and wherever you don't have matching order in table_b the fields will be null
SELECT table_a.*
FROM table_a
LEFT JOIN table_b
ON table_a.code_field = table_b.code_field
AND table_b.id IS NULL
You can do it with NOT EXISTS:
SELECT *
FROM table_a
WHERE NOT EXISTS (
SELECT 1 FROM table_b
WHERE table_a.code_field = table_b.code_field
)
table_a (item_id, item_desc)
table_b (item_id, item_name)
table_c (item_name, item_desc)
I need to set table_a.item_desc=table_c.item_desc
No matter what am I doing I'm still getting errors.
This is my final work:
UPDATE table_a
SET table_a.item_desc = table_c.item_desc
FROM table_a
INNER JOIN table_b
ON table_a.item_id = table_b.item_id
INNER JOIN table_c
ON table_b.item_name = table_c.item_name;
You have syntax error. In MySQL you don't need to add FROM clause when using UPDATE with JOIN.
Also add table alias for better readability.
UPDATE table_a A
INNER JOIN table_b B ON A.item_id = B.item_id
INNER JOIN table_c C ON B.item_name = C.item_name
SET A.item_desc = C.item_desc ;
In MySQL the join part goes right after the update keyword, not to the end of the statement:
UPDATE table_a
INNER JOIN table_b ON table_a.item_id = table_b.item_id
INNER JOIN table_c ON table_b.item_name = table_c.item_name
SET table_a.item_desc = table_c.item_desc;
Try this as in Update JOIN will be used before SET
UPDATE table_a
INNER JOIN table_b ON table_a.item_id = table_b.item_id
INNER JOIN table_c ON table_b.item_name = table_c.item_name
SET table_a.item_desc = table_c.item_desc;
I have 2 table
Columns inside table_A : data, id_A, id_B, status
Columns inside table_B : id, username
I want to display username from table_B reference from columns inside table_A (id_A & id_B) and make the alias id_A as User_1 and id_B as User_2
I has been working with INNER JOIN but it still make me confused
Can you try this...
SELECT a.username as User_1, b.username as User_2 FROM
table_A t
JOIN table_B b on b.id = t.id_B
JOIN table_B a ON a.id = t.id_A
I'd like to perform a MySQL query such that:
SELECT * FROM table_A JOIN table_B on table_A.id = table_B.foreign_key
…but I'd like to return rows where there is no match in table_B for table_A. Is this possible? How can I accomplish it?
You want to use a LEFT OUTER JOIN and then a WHERE clause to only allow NULL on the joined table.
SELECT * FROM table_A
LEFT OUTER JOIN table_B ON table_A.id = table_B.foreign_key
WHERE table_B.foreign_key IS NULL
Try this:
SELECT *
FROM table_A
LEFT JOIN table_B on table_A.id = table_B.foreign_key
WHERE table_B.foreign_key IS NULL