mysql copy data from one table to another using foreign key - mysql

inputPreviewOffsetLeft, inputPreviewOffsetTop, inputPreviewSizeWidth, inputPreviewSizeHeight are present in the display_preview table and display. display_preview has a foreign key displayId on display table.
INSERT INTO display_preview b (b.inputPreviewOffsetLeft, b.inputPreviewOffsetTop, b.inputPreviewSizeWidth, b.inputPreviewSizeHeight)
SELECT bd.inputPreviewOffsetLeft, bd.inputPreviewOffsetTop, bd.inputPreviewSizeWidth, bd.inputPreviewSizeHeight
FROM display
INNER JOIN display bd on bd.id = b.displayId
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'b (b.inputPreviewOffsetLeft, b.inputPreviewOffsetTop, b.inputPreviewSizeWidth...' at line 1

You don't need to get INSERT table an alias name and I think you might want to use INSERT INTO ... SELECT on display_preview and display
you can try the below query
INSERT INTO display_preview (inputPreviewOffsetLeft,inputPreviewOffsetTop, inputPreviewSizeWidth, inputPreviewSizeHeight)
SELECT bd.inputPreviewOffsetLeft, bd.inputPreviewOffsetTop, bd.inputPreviewSizeWidth, bd.inputPreviewSizeHeight
FROM display_preview b
INNER JOIN display bd on bd.id = b.displayId
EDIT
If you want to do UPDATE ... JOIN you can try this.
UPDATE display_preview b
INNER JOIN display bd on bd.id = b.displayId
SET
b.inputPreviewOffsetLeft = bd.inputPreviewOffsetLeft,
b.inputPreviewOffsetTop = bd.inputPreviewOffsetTop,
b.inputPreviewSizeWidth = bd.inputPreviewSizeWidth ,
b.inputPreviewSizeHeight = bd.inputPreviewSizeHeight
For more detail you can refer to MySQL UPDATE JOIN

Related

One table update accoriding to another table MySQL update error

I have two tables name activties and post_media now I want to update the media background color in activities table according to post media table record but when I run query it give me error.
Query
UPDATE A
SET A.bg_color = M.bg_color
FROM activities A
INNER JOIN post_media M ON A.relation_id = M.user_post_id AND A.media=M.file
WHERE A.relation_id>0
Error
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near 'FROM activities A INNER JOIN post_media M ON A.relation_id =
M.user_post_' at line 3
UPDATE syntax is different from SELECT. There is no FROM clause usage in UPDATE statement.
General flow is: UPDATE <table name> [JOIN <other tables>] SET ...
UPDATE activities A
INNER JOIN post_media M ON A.relation_id = M.user_post_id AND A.media=M.file
SET A.bg_color = M.bg_color
WHERE A.relation_id>0
Check documentation here for full syntax and further understanding: https://dev.mysql.com/doc/refman/8.0/en/update.html
Update query with use of join is different than SELECT query. Here you need to add tables before SET clause and all conditions in WHERE clause like SELECT.
e.g/
UPDATE t1, t2
SET t1.field = t2.field
WHERE condition 1
AND condition 2
So your query will be like as below:
UPDATE activities A, post_media M
SET A.bg_color = M.bg_color
WHERE A.relation_id = M.user_post_id
AND A.media=M.file
AND A.relation_id>0
Try this one.

PhpMyAdmin- Sharing data between two tables

I'm trying to populate a field id_owner of table1 (video_games) with the id from table2 (owner).
Field possessor of table1 contains the same data as field Name of table2.
I'm working with PhpMyAdmin.
What i 've already tried that was found under StackOverflow :
UPDATE t
SET t.`id_owner` = o.`id`
FROM `video_games` AS t
INNER JOIN
`owner` AS o
ON t.possessor = o.Name
Added quotes :
UPDATE t
SET t.`id_owner` = o.`id`
FROM `video_games` AS t
INNER JOIN
`owner` AS o
ON t.`possessor` = o.`Name`
UPDATE `video_games`
SET `id_owner` = `i`.`id`,
FROM (
SELECT `id`
FROM `owner`) `i`
WHERE
`i`.`Name` = `video_games`.`possessor`
without the aliases
UPDATE
`video_games`,
`owner`
SET
`video_games`.`id_owner` = `owner`.`id`,
WHERE
`video_games`.`possessor` = `owner`.`Name`;
Still get stuck with the same MySql error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE
`video_games`.`possessor` = `owner`.`Name``' at line 7
You are not selecting Name in the inner SELECT. It should be like this:
UPDATE video_games
SET id_owner = i.id
FROM (
SELECT id, Name
FROM owner) i
WHERE i.Name = video_games.possessor
I tried your idea but it resulted with the same error message.
Further research lead me to https://dev.mysql.com/doc/refman/5.5/en/update.html which proposition worked fine
UPDATE video_games,owner
SET video_game.id_owner=owner.id
WHERE video_games.id_possessor=owner.id;

UPDATE USING PHPMYADMIN

Good Afternoon,
I am trying to update a table using price data from another table however I get an error using an inner join. I am sure its something very stupid but having spent the best part of my day on this its time to ask for help.
If I do the following SELECT statement to test my inner join syntax works as it should
SELECT *
FROM polaracc_osrs_property_field_value
INNER JOIN polaracc_osrs_properties
ON polaracc_osrs_property_field_value.pro_id = polaracc_osrs_properties.id
WHERE polaracc_osrs_property_field_value.field_id =112
However when I then try and run an update statement using the price from one table to populate the 2nd I get the below error
1064 - You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'FROM polaracc_osrs_property_field_value INNER JOIN
polaracc_osrs_properties ' at line 3
The syntax used for the update statement is below
UPDATE polaracc_osrs_property_field_value
SET polaracc_osrs_property_field_value.value_integer = polaracc_osrs_properties.price
FROM polaracc_osrs_property_field_value
INNER JOIN polaracc_osrs_properties
ON polaracc_osrs_property_field_value.pro_id = polaracc_osrs_properties.id
WHERE polaracc_osrs_property_field_value.field_id = 112
Your join needs to happen before you set your values like this:
UPDATE polaracc_osrs_property_field_value
INNER JOIN polaracc_osrs_properties
ON polaracc_osrs_property_field_value.pro_id = polaracc_osrs_properties.id
SET polaracc_osrs_property_field_value.value_integer = polaracc_osrs_properties.price
WHERE polaracc_osrs_property_field_value.field_id = 112;
Hope this helps.

SQL Syntax error in Update from script?

I'm trying to do a fairly simple update of one column from one table to the same column in another.
I have two tables:
container (id, barcode_1, location)
test(id, barcode_1, location)
Both tables are exactly the same except for the barcode_1 column. I want to update container.barcode_1 with the values from test.barcode_1 on all matching ids.
This is the code I'm using:
update container
set container.barcode_1 = test.barcode_1
FROM container INNER JOIN test ON container.id = test.id;
When I run that code, I get this error:
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM container INNER JOIN test ON container.id = test.id' at line 3
Any ideas one why this might be failing?
Your UPDATE syntax is wrong here. It should be
update container
INNER JOIN test ON container.id = test.id
set container.barcode_1 = test.barcode_1;
(OR) Using table alias
update container c
INNER JOIN test t ON c.id = t.id
set c.barcode_1 = t.barcode_1;

Update a column in a database with values from another colum of another database

I need to update a column in a database with values from another column of another database.
Here is my query:
UPDATE dbA.tableA as a
SET a.columnA = b.columnB
FROM dbB.tableB as B
WHERE
a.num = b.num
And I get the error:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM ...
Both databases are in the same server.
How can I solve this?
The correct format of update with join is
UPDATE dbA.tableA a
join dbB.tableB b on a.num = b.num
SET a.columnA = b.columnB