Syntax error Mysql - 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

Related

What is the correct syntax?

I wrote a sql query to update data of 'tbl_products'.
Here is the query
update tbl_products
set product_count = (product_count - tbl_order_details.product_sales_quantity)
from tbl_products
join tbl_order_details on tbl_order_details.product_id = tbl_products.product_id
join tbl_order on tbl_order.order_id = tbl_order_details.order_id
where tbl_order.order_id = 54;
But it gives me the following error
"#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 'from tbl_products join tbl_order_details on tbl_order_details.product_id = tbl_p' at line 1"
Whats wrong here?
In MySQL, the correct syntax is:
update tbl_products p join
tbl_order_details od
on od.product_id = p.product_id join
tbl_order o
on o.order_id = od.order_id
set p.product_count = (p.product_count - od.product_sales_quantity)
where o.order_id = 54;

Errors While Using IF statement in sql query

I am trying to make a query, which returns tipo_id from a table, depending on the value of this I want to join with another table, for example if tipo_id is 1 I want to join with table called p_read if tipo_id i want to join tv_read
this is what I tried to do.
SELECT ec.id,ec.estado,fv.id,fv.num_factura,fv.importe,fv.iva,fv.total,fv.fecha_consumo_inicio,fv.fecha_consumo_fin,
fv.fecha_factura, fv.fichero, c.total, l.tipo_id, lp.id_consumo FROM aldroges8.factura_venta fv
INNER JOIN aldroges8.lectura l ON fv.id=l.facturaVenta_id
INNER JOIN aldroges8.factura_cobro fc ON fc.facturaventa_id = fv.id
INNER JOIN aldroges8.cobros c ON c.id=fc.cobros_id
INNER JOIN aldroges8.estado_cobros ec ON ec.id = c.estado
IF (l.tipo_id=1)
INNER JOIN aldroges8.lectura_potencia lp ON l.id=lp.id
ELSE IF (l.tipo_id =3)
INNER JOIN aldroges8.lectura_tv_gas lp ON lp.id=l.id
WHERE fv.factura_enviada=1 AND fv.suministro_id=:id_contrato ORDER BY fv.fecha_factura DESC;
But i am getting this error.
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 'IF (l.tipo_id==1)
INNER JOIN aldroges8.lectura_potencia lp ON l.id=lp.id
ELSE' at line 7
So I want to know if there is a way on doing this if staments on a query, or do I need to make another query with tipo_id, thanks in advance
SELECT ec.id,ec.estado,fv.id,fv.num_factura,fv.importe,fv.iva,fv.total,fv.fecha_consumo_inicio,fv.fecha_consumo_fin,
fv.fecha_factura, fv.fichero, c.total, l.tipo_id,
/* Used case when statement to get the required result in that column */
case when l.tipo_id=1 then lp_1.id_consumo
when l.tipo_id=3 then lp_3.id_consumo end as id_consumo
FROM aldroges8.factura_venta fv
INNER JOIN aldroges8.lectura l ON fv.id=l.facturaVenta_id
INNER JOIN aldroges8.factura_cobro fc ON fc.facturaventa_id = fv.id
INNER JOIN aldroges8.cobros c ON c.id=fc.cobros_id
INNER JOIN aldroges8.estado_cobros ec ON ec.id = c.estado
left join aldroges8.lectura_potencia lp_1 ON l.id=lp_1.id
left join aldroges8.lectura_tv_gas lp_3 ON lp_3.id=l.id
WHERE fv.factura_enviada=1 AND fv.suministro_id=:id_contrato ORDER BY fv.fecha_factura DESC;
I would write this with the condition in the on clause and then use coalesce() in the select:
SELECT ec.id, ec.estado, fv.id, fv.num_factura, fv.importe, fv.iva,
fv.total, fv.fecha_consumo_inicio, fv.fecha_consumo_fin,
fv.fecha_factura, fv.fichero, c.total, l.tipo_id,
coalesce(lp_1.id_conumo, lp_3.id_consumo) as id_consumo
FROM aldroges8.factura_venta fv INNER JOIN
aldroges8.lectura l
ON fv.id = l.facturaVenta_id INNER JOIN
aldroges8.factura_cobro fc
ON fc.facturaventa_id = fv.id INNER JOIN
aldroges8.cobros c
ON c.id = fc.cobros_id INNER JOIN
aldroges8.estado_cobros ec
ON ec.id = c.estado LEFT JOIN
aldroges8.lectura_potencia lp_1
ON l.id = lp_1.id AND l.tipo_id = 1 LEFT JOIN
aldroges8.lectura_tv_gas lp_3
ON lp_3.id = l.id AND l.tipo_id = 3
WHERE fv.factura_enviada = 1 AND
fv.suministro_id = :id_contrato
ORDER BY fv.fecha_factura DESC;
The difference between doing the comparison in the ON verses in a CASE expression may seem subtle, but it can be important.
If there are multiple matches in either table, then putting the condition in the SELECT will result in duplicate rows.

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 ;

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

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

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'