How can I update filed value from a select with inner join? - mysql

I am trying to update a field value in a mysql database using a select query with inner join
I currently get
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 'AS cc WHERE cc.account_id = na.account_id' at line 5
UPDATE accounts AS na
SET na.pdm_id = (
SELECT cp.person_id FROM `temp_accounts` AS ta INNER JOIN call_managment_system.accounts AS a ON ta.company_code = a.company_code
INNER JOIN contact_personal AS cp ON cp.name = ta.FSM AND contact_link = 'PDM'
)
WHERE a.account_id = na.account_id
How can I fix this query to work? I want to update the field called pdm_id to set it equal to cp.person_id
Thanks

UPDATE accounts na
INNER JOIN call_managment_system.accounts a
ON a.account_id = na.account_id
INNER JOIN temp_accounts ta
ON ta.company_code = a.company_code
INNER JOIN contact_personal cp
ON cp.name = ta.FSM
SET na.pdm_id = cp.person_id
WHERE contact_link = 'PDM'

Related

Declaring a variable in MySQL with multiple JOIN tables

I am trying to get a resulting column of the initial month an ID was created where multiple table JOINs are needed in MySQL Workbench.
SET #in_month = '0';
SELECT
ca.id
FROM capital.user ca
JOIN
capital.user_account cd on ca.id = cd.user_id
JOIN
capital.transaction ct on cd.user_id = ct.user_account_id
JOIN
capital.transaction_event ce on ct.id = ce.auth_entry_id
#in_month = month(ce.created) WHERE ce.message = 'Approved'
Group by id;
I get Syntax error: '#in_month' (at text suffix) is not valid input at this position on line 17, any ideas of what I might be doing wrong? I don't have a lot of experience with SQL
you are missing semicolon after variable declaration & and on join condition
SET #in_month = '0';
SELECT
ca.id
FROM capital.user ca
JOIN capital.user_account cd on ca.id = cd.user_id
JOIN capital.transaction ct on cd.user_id = ct.user_account_id
JOIN capital.transaction_event ce on ct.id = ce.auth_entry_id and
#in_month = month(ce.created)
WHERE ce.message = 'Approved'
Group by id;

i've got error when use Inner join with 3 tables in mysql

hello i have search too many and i checked my query word by word but i can't fix this error
my query :
SELECT color_table.color, size_table.size, size_table.length, size_table.sum, image_table.image_url, manto_table.name, manto_table.description, manto_table.price_sale, manto_table.price_coop,
manto_table.price_single FROM size_table INNER JOIN
color_table ON size_table.color_id = color_table.id INNER JOIN
manto_table INNER JOIN
image_table ON manto_table.id = image_table.manto_id ON size_table.manto_id = manto_table.id;
error :
Type: PDOException
Code: 42000
Message: SQLSTATE[42000]: Syntax error or access violation: 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 'ON size_table.manto_id = manto_table.id' at line 5
SELECT color_table.color,
size_table.size,
size_table.length,
size_table.sum,
image_table.image_url,
manto_table.NAME,
manto_table.description,
manto_table.price_sale,
manto_table.price_coop,
manto_table.price_single
FROM size_table
LEFT JOIN color_table AS color_table
ON size_table.color_id = color_table.id
LEFT JOIN manto_table AS manto_table
ON manto_table.id = image_table.manto_id
LEFT JOIN image_table AS image_table
ON size_table.manto_id = manto_table.id;
modify it like this:
SELECT color_table.color, size_table.size, size_table.length, size_table.sum, image_table.image_url, manto_table.name, manto_table.description, manto_table.price_sale, manto_table.price_coop,
manto_table.price_single
FROM size_table
INNER JOIN color_table ON size_table.color_id = color_table.id
INNER JOIN manto_table ON manto_table.id = image_table.manto_id
INNER JOIN image_table ON size_table.manto_id = manto_table.id;
Re-organize your sentence to:
SELECT color_table.color, size_table.size, size_table.length, size_table.sum, image_table.image_url,
manto_table.name, manto_table.description, manto_table.price_sale, manto_table.price_coop, manto_table.price_single
FROM size_table
INNER JOIN manto_table ON size_table.manto_id = manto_table.id
INNER JOIN image_table ON manto_table.id = image_table.manto_id
INNER JOIN color_table ON size_table.color_id = color_table.id ;

MySQL query to SQL Server query

I defined sql query and it runs with no problem on MySQL (I am using MySQL) , but when I am trying to execute it on client site (they uses SQL Server)
I am getting "Error: Incorrect syntax near 'si'." error message
Hope someone can help me to define right syntax.
The query is following:
update stepinstance si
inner join cesteplink l on si.id = l.stepinstance_id
inner join prompt p on si.prompt_id = p.id
set si.principal_id = 29160180
where l.case_id = 29179541
and si.principal_id = 1799409
and si.status = 'In Progress'
set has to be before the join conditions.
update si
set si.principal_id = 29160180
from stepinstance si
inner join cesteplink l on si.id = l.stepinstance_id
inner join prompt p on si.prompt_id = p.id
where l.case_id = 29179541
and si.principal_id = 1799409
and si.status = 'In Progress'

Error with my UPDATE mysql query, with a lot of join

When I try the following query :
UPDATE cache_implementation
SET parent_through_compared_id = ncp.nid, parent_through_feature_id = nfp.nid
FROM cache_implementation n
INNER JOIN cache_compare nc ON n.compared_id = nc.nid
INNER JOIN cache_implementation ncp ON (nc.nid = ncp.compared_id AND n.feature_id = ncp.feature_id)
INNER JOIN cache_feature nf ON n.feature_id = nf.nid
INNER JOIN cache_implementation nfp ON (nf.nid = nfp.feature_id AND n.compared_id = nfp.compared_id)
I have the following 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 'FROM cache_implementation n INNER JOIN cache_compare nc ON n.compared_id = nc' at line 3
Through this query, I try to update two fields with value located in some other table, by making a mass update query.
You are using used in TSQL. Here's for MySQL.
UPDATE cache_implementation n
INNER JOIN cache_compare nc
ON n.compared_id = nc.nid
INNER JOIN cache_implementation ncp
ON (nc.nid = ncp.compared_id AND n.feature_id = ncp.feature_id)
INNER JOIN cache_feature nf
ON n.feature_id = nf.nid
INNER JOIN cache_implementation nfp
ON (nf.nid = nfp.feature_id AND n.compared_id = nfp.compared_id)
SET parent_through_compared_id = ncp.nid, parent_through_feature_id = nfp.nid
In MySQL multi-table update statement, the SET clause follows the table references. (This differs from the syntax used in other databases.)
To fix your statement, delete that first line, move the line with SET to the bottom, qualify the column references with the table alias, and change FROM to UPDATE. Voila.
UPDATEcache_implementation
FROM cache_implementation n
INNER JOIN ...
INNER JOIN ...
SET n.col = expr, n.col2 = expr
Multi-table update syntax documented here: http://dev.mysql.com/doc/refman/5.5/en/update.html

Syntax error Mysql

No Idea why this is not working - could someone please help?
update c set c.images = ""
from j17_content c
inner join j17_jreviews_content rc on rc.contentid = c.id
inner join j17_categories cat on cat.id = c.catid
where cat.path like "cakes%"
and c.created_by in (62,63,99)
and rc.email = 'email'
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 j17_content c inner join j17_jreviews_content rc on rc.contentid = c.id in' at line 2
UPDATE:
Now trying
UPDATE j17_content c SET c.images=''
inner join j17_jreviews_content rc on rc.contentid = c.id
inner join j17_categories cat on cat.id = c.catid
where cat.path like 'cakes%'
and c.created_by in (62,63,99)
and rc.email = 'email'
still getting
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 'inner join j17_jreviews_content rc on rc.contentid = c.id inner join j17_catego' at line 2
UPDATE doesn't take a FROM.
The syntax should be UPDATE j17_content c SET c.images="" INNER JOIN ...
This is essentailly your code.
Try it this way:
update j17_content c
inner join j17_jreviews_content rc on rc.contentid = c.id
inner join j17_categories cat on cat.id = c.catid
where cat.path like 'cakes%'
and c.created_by in (62,63,99)
and rc.email = 'email'
set c.images = '';
This is an UPDATE JOIN
I have a refactored version of the query in mind
update
(
SELECT content_id id
FROM j17_jreviews_content
WHERE email = 'email'
) rc
inner join j17_content c USING (id)
inner join
(
SELECT id catid
FROM j17_categories
WHERE created_by in (62,63,99)
AND path like 'cakes%'
) cat USING (catid)
set c.images = '';
You will need indexes for the subqueries involved
ALTER TABLE j17_categories ADD INDEX created_by_path_id_ndx (created_by,path,id);
ALTER TABLE j17_jreviews_content ADD INDEX email_content_id_ndx (email,content_id);
replace your double quotes(") with single ones(')
Sorry this is not really the way you guys suggested - but I got a list of IDs and updated them using no joins with an in statement of all the IDs