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
Someone in my company downloaded some data, played with it in Excel and uploaded again.
Excel when trying to be helpful truncated a leading zero on a file called license_number.
As a result rather than having "037463524" the data now says "37463524"
I know that if the string is eight characters long, I need to add a "0" to the front of it to correct the mess.
Is there a SQL query that I can run in order to accomplish this?
You can use LENGTH()
UPDATE Tablename SET license_number = '0' + license_number WHERE LENGTH(license_number) = 8
or
UPDATE Tablename SET license_number = CONCAT('0', license_number) WHERE LENGTH(license_number) = 8
One more way by using LPAD
UPDATE `TABLE` SET `Lic_NO` = LPAD(`Lic_NO`, 9, '0')
I'm a beginner MySQL user.
My teacher gave me a question to explain how this function works.
SELECT *
FROM TableName
WHERE ColumnName=ColumnName
Then, the result shows the rows that contain values in that column. (The null value is not appear)
I have no idea about it. I do searching for answer but most of it is talking about using IS NOT NULL.
You need to use ' = ' to select rows by checking an empty column right ?
If that's the case
You can simply use the below code
SELECT *
FROM TableName
WHERE ColumnName = ' '
The expression:
WHERE ColumnName = ColumnName
is comparing two values from the same column. This should be true in all cases, except when ColumnName contains a NULL value. So, you can equivalently write this as:
WHERE ColumnName IS NOT NULL
This version is more understandable and the preferred way to write the logic.
I have in Mytable some value = 'NA'
Insted of this value I would like to put NULL.
So I've write:
INSERT INTO Mytable
VALUES(NULL)
WHERE VALUES('NA');
But I didn't work.
I didn't put the name of the column because potentially all column can have some 'NA' value.
I hope someone have a idea to do it.
Regards
Sam
UPDATE Mytable
SET value = NULL
WHERE value = 'NA';
Yes, you must do this for each column/attribute that you want to update.
UPDATE Mytable SET value = NULL WHERE value = 'NA'
To replace occurrences of 'NA' with NULL in multiple columns, for all rows in a table, you can do this in a single update query. The trick is to assign the current value of the column back to the column when you don't want the value changed.
For example:
UPDATE Mytable t
SET t.column_one = IF(t.column_one='NA',NULL,t.column_one)
, t.column_two = IF(t.column_two='NA',NULL,t.column_two)
, t.column_fee = IF(t.column_fee='NA',NULL,t.column_fee)
WHERE t.column_one = 'NA'
OR t.column_two = 'NA'
OR t.column_fee = 'NA'
NOTES:
Repeat the column assignment for each column you need to do the replacement. (The example above references three columns, named column_one, column_two and column_fee. I don't know the names of the columns in your table; you would need to replace those references with the actual names of the columns in your table.)
The WHERE clause is optional; the query would have the same net result without that WHERE clause. (Without the WHERE clause, the query would update every row in the table; any rows that don't have an 'NA' in one of the three columns would not be changed, since the columns will all be assigned their current values.
For a lot of columns, it's more efficient to do it in a single operation, to apply several changes to a row in one statement, rather than separate statements each making updates to the same row.)
The expresssion IF(a,b,c) evaluates expression a as a boolean; if it returns TRUE, it returns expression b, otherwise it returns expression c.
To see how this works, you can run a SELECT statement (remove the SET clause, and replace the UPDATE keyword with SELECT and relevant expressions in the SELECT list:
For example:
SELECT t.column_one AS _one_old
, IF(t.column_one='NA',NULL,t.column_one) AS _one_new
, t.column_two AS _two_old
, IF(t.column_two='NA',NULL,t.column_two) AS _two_new
, t.column_fee AS _fee_old
, IF(t.column_fee='NA',NULL,t.column_fee) AS _fee_new
FROM Mytable t
WHERE t.column_one = 'NA'
OR t.column_two = 'NA'
OR t.column_fee = 'NA'
The _old columns return the existing values in the columns; the _new columns return the value that would be assigned (by the UPDATE statement earlier in my answer.)
The results from that query will verify that IF() expressions will return a NULL when the existing value in the column is 'NA'; it will also confirm that the IF() expression will return the existing value when the existing value in the column is not 'NA'.
FOLLOWUP
With 20 different tables with 12 columns each, I'd make use of the information_schema.columns table in MySQL to help me generate the required expressions.
Something like this:
SELECT CONCAT(' , t.'
,c.column_name,' = IF(t.'
,c.column_name,'=''NA'',NULL,t.'
,c.column_name,')') AS expr
FROM information_schema.columns c
WHERE c.table_schema = 'mydatabase' -- the name of your database
AND c.table_name = 'mytable' -- the name of your table
AND c.data_type IN ('varchar','char') -- only character type columns
ORDER BY c.ordinal_position
Which will return something like this:
expr
-------------------------------------
, t.fee = IF(t.fee='NA',NULL,t.fee)
, t.fi = IF(t.fi='NA',NULL,t.fi)
, t.fo = IF(t.fo='NA',NULL,t.fo)
, t.fum = IF(t.fum='NA',NULL,t.fum)
So, this doesn't actually update the table, it's just a convenient way to avoid typing out a bunch of SQL expressions. You can copy that result set, and use it to form a statement similar to the one I showed in my answer above. (Obviously, you would omit rows that you don't want to change, and the first comma would need to be changed to the SET keyword, and the rest of the statement would need to be wrapped around this.
Personally, I wouldn't bother with a WHERE clause, because if it's a lot of columns, the query is going to do a full scan of the table anyway.
SELECT CONCAT(' OR t.',c.column_name,' = ''NA''') AS expr
FROM information_schema.columns c
WHERE c.table_schema = 'mydatabase' -- the name of your database
AND c.table_name = 'mytable' -- the name of your table
AND c.data_type IN ('varchar','char') -- only character type columns
ORDER BY c.ordinal_position
This will return something like:
expr
----------------------
OR t.fee = 'NA'
OR t.fi = 'NA'
OR t.fo = 'NA'
OR t.fum = 'NA'
CAUTION: be careful that you don't do comparison of numeric columns to 'NA', because MySQL will evaluate 'NA' as a numeric value of zero (0) in a numeric context.
I can't find the answer since searching mysql NOT in google is a nightmare (even with the quotes).
I need to make a query like this:
SELECT * FROM table WHERE field=value AND field2!=value2 AND field3!=value3
How it is done? Is it even possible?
SELECT * FROM table WHERE
((field = value) AND
(field2 <> value2) AND
(field3 <> value3))
If you're dealing with NULL, you have to do two things:
Use SET ANSI_NULLS ON
Declare NULL values to a dummy value.
SQL Cannot compare nulls.
To do that:
SET #value = ISNULL(#value, -1);
Yes, you can do exactly what you wrote, but use <> instead of !=
Perhaps the answer depends on what "value" is? For example, for an integer 123 value would be 123; for a string "foobar" value would be 'foobar'.
have you tried the <> operator
SELECT * FROM table WHERE field = value AND field2 <> value2
have you tried "<>"? it works in Delphi