MySQL Update query is not working with and operator - mysql

First query:
update tableA set columnA = 'Hello' and updated_at = now() where id = 10;
Second query:
update tableA set columnA = 'Hello', updated_at = now() where id = 10;
When I execute first query columnA updated as 0, where as second query worked fine and updated as Hello.
Why first query update the table as 0 value.

I think that MySQL's lax syntax is at work here. Consider rewriting your first update as:
UPDATE tableA
SET columnA = ('Hello' AND updated_at = NOW())
WHERE id = 10;
That is, the expression on the RHS being assigned to columnA is actually the AND of a string literal, and an assignment. Check the demo below to verify that this RHS in fact is evaluating to zero.
Demo
As to exactly why this is happening, we would have to lookup MySQL's rules for what happens. But best practice is to just stick with your second update query, which uses correct ANSI syntax.

#TimBiegeleisen is right, there is only one assigning expression for your first query.
MySQL syntax for SET assignment_list in UPDATE is like
assignment [, assignment] ...
every assigning expression for column separated by comma(,)
So, when you have multiple assignment use comma(,) to separate the assignment.
You found more details doc here

Related

MySQL - IF Query with STR_TO_DATE

Greetings Stackian Overflowers,
I'm trying to overwrite data in an existing column and do a STR_TO_DATE in the case it is already set.
Here's the code :
SELECT IF(columnA = '../../..' , '0000/00/00' , STR_TO_DATE(columnA, '%m/%d/%Y)
FROM tablename
WHERE extract_date = '2018-12-31';
In all reality, I must use this logic to make an update on the table. I tried this :
UPDATE tablename
IF columnA = '../../..' THEN
SET columnA = '0000-00-00' ELSE
SET columnA = STR_TO_DATE(columnA,'%m/%d/%Y);
Could use any help on the matter.
MySQL's REGEXP operator comes in handy here:
UPDATE tablename
SET date_col = CASE WHEN columnA REGEXP '[0-9]{2}/[0-9]{2}/[0-9]{4}'
THEN STR_TO_DATE(columnA, '%m/%d/%Y)
ELSE NULL END;
It doesn't make sense to update columnA from a string to a date, because the types don't match. A better strategy is to create a new column to store the date. Also, I recommend using NULL, not 0000-00-00, to represent a date string that could not be parsed.
Note that the regex I used is not foolproof and does not completely validate your date strings. But it should at least weed out string data which is really off.

mysql where clause field

I am trying to understand a piece of mysql code:
Select * from tableA
where type = 'blue'
and status = 'confirmed'
and statement
and date between '2017-01-01' and '2017-12-31'
Would would the "and statement" mean where statement is a field but without an =, or, and, >, < ect.
Thanks in advance
This is a MySQL peculiarity that other database engines do not exhibit. In other DBMS the equivalent would be:
and statement<>0
An empty where condition, as above, is effectively the same as AND LENGTH(statement) > 0. So any non-empty value in the statement column will be returned.

Subquery returns more than 1 row. Using SQL select to update a different tables results

I am trying to update a column in the following table 'jobqueue' using the results from a SELECT query performed on the 'mdtinfo' table.
The column I am trying to update is called ignore and I need to set the value to 1 from its default of 0.
update jobqueue
set jobqueue.`ignore`= '1'
where (SELECT JobQueue_job_queue_id
FROM mdtinfo
WHERE product_name = 'Example')
The above query returns the following error: SQL Error (1242): Subquery returns more than 1 row.
When running the select query on it's own it returns results successfully.
In MySQL, a value of zero appearing in a WHERE clause means false.
So, UPDATE something SET col=val WHERE (SELECT colx FROM sometable) has the potential to be a valid query. If the inner SELECT gets just one row, and its colx column has the value 0, the update won't do anything. If the colx column has a nonzero value the query means UPDATE something SET col=val WHERE true. Accordingly, every row in sometable will be updated. I doubt that's what you want.
If the inner SELECT happens to return more than one row, the query isn't valid. You'll get the error 1242 you actually received.
(This business of interpreting numbers as Boolean values causes MySQL to accept some otherwise dodgy query syntax, like the syntax in your question.)
I guess you want to retrieve the job_queue_id values for the row or rows you actually want to update. So try something like this.
update jobqueue
set jobqueue.`ignore`= '1'
where jobqueue.job_queue_id IN (SELECT JobQueue_job_queue_id
FROM mdtinfo
WHERE product_name = 'Example')
I guessed you have a column jobqueue.job_queue_id. You didn't tell us what columns you have in jobqueue.
update jobqueue
set jobqueue.`ignore`= '1'
where jobqueue.`job_queue_id` IN (SELECT GROUP_CONCAT(JobQueue_job_queue_id)
FROM mdtinfo
WHERE product_name = 'Example' GROUP BY product_name)
you should write column name in where condition.

Get Error SQL Server Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >=

I run the following query
CREATE TRIGGER ubahjumlahupah ON tbl_pkrjbhn
AFTER UPDATE
AS
UPDATE tbl_hitash SET
jumlah_upah = (SELECT (tbl_pkrjbhn.harga_satuan*tbl_hitash.upah_hitash)
FROM tbl_hitash, tbl_pkrjbhn
WHERE tbl_hitash.id_pkrjbhn = tbl_pkrjbhn.id_pkrjbhn)
FROM tbl_hitash
but i get error :(
The problem is clear: your SELECT result returns more than one value, so the query results in:
CREATE ...
...
UPDATE ... SET
jumlah_upah = (more than one value)
So you need to make sure the SELECT command returns a single value so that it can be assigned to the jumlah_upah variable.

update a table based on the outcome of a join in MySQL

I understand the syntax here:
UPDATE table
SET column1 = expression1,
column2 = expression2,
...
WHERE conditions;
and here:
UPDATE table1
SET column1 = (SELECT expression1
FROM table2
WHERE conditions)
WHERE conditions;
...but what if I want to update
UPDATE table1
SET column1 = expression
if we get a particular result on a join between table1 and table2
which has WHERE conditions?
I can't seem to figure it out, and I get syntax errors in all of my attempts. Any advice greatly appreciated.
The syntax in MySQL is:
UPDATE table1 JOIN
table2
ON conditions
SET table1.column1 = table2.expression1
WHERE conditions;
What I do is write it as a SELECT statement first.
SELECT t.id
, t.col AS old_val
, s.expr AS new_val
FROM target_table t
JOIN source_table s
ON s.somecol = t.somecol
AND s.othercol < 1
WHERE s.something_else = 'abc'
In this example, t.col is the column that I (eventually) want to assign a new value to. (Used in the SELECT statement here, this just displays the value currently stored in the column.)
The expression s.expr represents the expression that returns the value I want to assign to col. This could be as simple as a column reference, or can be a more complex expression.
The rest of the statement are the normal FROM, JOIN, ON and WHERE clauses we're familiar with.
Once I have a SELECT statement that is working, I can convert that into an UPDATE statement by
replacing SELECT ... FROM (at the beginning of the statement) with the keyword UPDATE
adding a SET clause before the WHERE clause.
For example, I would convert the SELECT statement above to something like this, to assign the value of s.expr to t.col:
UPDATE target_table t
JOIN source_table s
ON s.somecol = t.somecol
AND s.othercol < 1
SET t.col = s.expr
WHERE s.something_else = 'abc'
This the approach that works for me. Writing it as a SELECT first allows me to test, muck with the conditions and expressions, and review to verify which rows are going to be updated, what values are being replaced, and what values are going to be assigned to each row.