Mysql Updating with if condition within a Table - mysql

I am unable to update 2 fields in a Table using UDPATE AND IF condition. Need help.
I have an invoice table where in If field Nos=0 I need to update field Qty from Capacity and field Nos=1 in the same table.
My sql statement is not working:
UPDATE INVDTLS_draft1 SET `Nos`=1, `Qty`=`Capacity` IF (Nos=0) WHERE id=id
error message:
#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 (Nos=0) WHERE id=id' at line 1

try this query, using AND operator
UPDATE INVDTLS_draft1 SET `Nos`=1, `Qty`=`Capacity` WHERE id=id AND Nos=0

Why id=id? I think you need this:
UPDATE INVDTLS_draft1
SET `Nos`=1, `Qty`=`Capacity`
WHERE Nos=0

Try this:
UPDATE INVDTLS_draft1 SET
Nos=1,
Qty = if(nos=0, capacity, Qty)
WHERE id=?
This "does nothing" to qty if nos != 0.

Related

MySql update query not working-generic error

I get a generic error message, and have no idea what the problem is with the query. What do I do to fix this?
Query explanation: there are two tables, Invoice, and temp. I need to take zip codes from temp table and push them to the Invoice table, based on the invoice number.
START TRANSACTION
UPDATE
Invoice
SET
Invoice.zip_code = (SELECT zip_code FROM temp WHERE temp.invoice_number = Invoice.invoice_number)
WHERE
Invoice.invoice_date >= '2017-08-01'
ROLLBACK
this is the 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 'UPDATE Invoice SET Invoice.zip_code = (SELECT
zip_code FROM temp WHERE temp' at line 3
Add semicolon after each command.
START TRANSACTION;
UPDATE
Invoice
SET
Invoice.zip_code = (SELECT zip_code FROM temp WHERE temp.invoice_number = Invoice.invoice_number)
WHERE
Invoice.invoice_date >= '2017-08-01';
ROLLBACK;
Sounds like #Daniel Blais is right. One thing you can do to troubleshoot is to break the query down and run each part individually. That will help you find out what's wrong.
SELECT zip_code FROM temp WHERE temp.invoice_number = Invoice.invoice_number;
One other question: Why are you rolling back after the table is updated? Don't you want to commit?

Update longtext field in mysql

Im trying to update a longtext type field called 'comment' using a simple sql query in mysql client like this :
Update mytable set comment='Test' where id = 1;
But i'm getting this error
ERROR 1064 (42000): 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 'comment='Test' where id = 1' at line 1
Am i missing something ?, thanks in advance.
comment is a reserved word, if you want to have a table/field with that name, you have to quote it (or use the table.fieldname syntax, in case of a field). default in mysql is the backtick for that, so:
update mytable set `comment`='Test' where id = 1;
Found it, it gets solved with this:
update mytable as a set a.comment='Test' where id = 1;

delete query for mysql using c

can anyone tell me the correct query to delete values from mysql db table,in my case the table name and id are accepted from the user and the row is deleted based on id.This is my query
sprintf(Query,"DELETE FROM ('%s') where id = (%d)",tb1,idt1) ;
/*table name is in form of string and id is int */
mysql_query(conn,Query);
You should remove parentheses around the table name:
sprintf(Query,"DELETE FROM '%s' where id = (%d)",tb1,idt1) ;
MySQL considers queries like this syntax errors:
delete from (mytable) where id=2;
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 '(mytable) where id=2' at line 1
(I'll assume that you know everything about SQL injection attacks, and that neither tb1 nor idt1 are constructed from user input in any shape or form).

MySQL foreign key problem

I'm getting that error running on MySQL 5.5.8
Mysql2::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
(SELECT id FROM products WHERE id = NEW.brand_id) IS NULL;
EN' at line 6:
CREATE TRIGGER fk_brands_products_insert
BEFORE INSERT ON brands
FOR EACH ROW BEGIN
SELECT
RAISE(ABORT, "constraint violation: fk_brands_products")
WHERE
(SELECT id FROM products WHERE id = NEW.brand_id) IS NULL;
END;
What could be wrong?
I suspect the problem is there is no FROM clause in your select statement.
Are you sure you can raise errors inside a query like that? I can't find it anywhere. I think the proper way would be to select a COUNT or EXISTS and return the result of that INTO a variable. Then, after the query, raise an error if the result doesn't meet your expectations.
Something like this:
SELECT count(id) INTO IDCOUNT FROM products WHERE id = NEW.brand_id;
Wouldn't it be better by the way to just add a real constraint? Or do you use a storage type that doesn't support that?

Modifing select query to delete query

The following select query works fine:
SELECT * FROM JBPM_JOB job WHERE job.ACTION_ IN (SELECT ID_ from JBPM_ACTION WHERE ACTIONEXPRESSION_ LIKE '%#{reminderAction.addAsyncProcessReminder%warning%');
However, when I try to delete the rows retrieved here, it fails
DELETE FROM JBPM_JOB job WHERE job.ACTION_ IN (SELECT ID_ from JBPM_ACTION WHERE ACTIONEXPRESSION_ LIKE '%#{reminderAction.addAsyncProcessReminder%warning%');
What is wrong here?
The error message is:
ERROR 1064 (42000): 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 'job WHERE job.ACTION_ IN (SELECT ID_ from JBPM_ACTION WHERE ACTIONEXPRESSION_ LI' at line 1
You need to specify you are deleting from the alias table, so use:
DELETE job FROM JBPM_JOB job WHERE job.ACTION_ IN (SELECT ID_ from JBPM_ACTION WHERE ACTIONEXPRESSION_ LIKE '%#{reminderAction.addAsyncProcessReminder%warning%');
i have tested the query in sql server. works fine but there is possible that the values you are deleting have some relationship with other table like PK and FK.
if they have then you have to delete the records from those tables too..........