I have the following which only inserts to column two if the value is empty:
UPDATE name
SET one=?,
two=COALESCE(two, ?),
three=?
WHERE id = ?
However, this only works if column two if the value is null. How can I get this to work if it is either NULL OR an empty string.
It should always insert to one and three. It should only insert to two if it is NULL or empty
I am using SQLite with PHP
If by "empty" you mean an empty string, you can use nullif():
two = COALESCE(NULLIF(two, ''), ?)
The more general solution is a CASE expression.
Don't store empty strings in TEXT columns.
Update the table to replace all empty strings with NULLs:
UPDATE name
SET two = NULL
WHERE TRIM(two) = ''
Now you can use your query, which is correct and in any other query you only need to check for NULL.
Related
Need to update a field in database, but when I make a request, it writes me "0 rows affected" although the image field is, and it is empty.
UPDATE `oc_product` SET `image`= 'no_image.png' WHERE `image`='';
Your query is checking for a value of an empty string, although from what you have shown the value you are actually looking to change has a value of NULL. These are two different values, NULL isn't equal to ''. As such if you are looking to replace items that have a NULL value you need to change your where statement for that (WHERE image IS NULL):
UPDATE `oc_product` SET `image`= 'no_image.png' WHERE `image` IS NULL;
Did select query returns any rows
select image from oc_product WHERE image='';
If so add other field confition as well to check whether it is getting affected ot not example
UPDATE oc_product SET image= 'no_image.png' WHERE (image= "" AND image IS NULL) and other_column ='column value';
Hope this helpful for you :)
In my php code query, i got some error when i will place a null value
into the table. The column names "number1","number2", and "number3" are integers and can have a null value.
if there is no null value, the query works
query is like this
insert into table(number1,number2,number3) values (1,2,3);
but when I leave a blank value on the input form on the PHP,
for example I will not place value for column "number2", the query will look
like this.
insert into table(number1,number2,number3) values (1,,3);
it says ERROR: syntax error at or near ","
SQL state: 42601
Does anybody have an idea how to solve this?
this query is likely to be the solution but is there any other way?
insert into table(number1,number2,number3) values (1,null,3);
because i got so many variables like that and am a bit lazy to place some conditions like when that variable returns a blank value then value="null"
You insert NULL value by typing NULL:
INSERT INTO table(number1,number2,number3) VALUES (1,NULL,3);
If you have a variable and when that variable is empty you want to insert a NULL value you can use NULLIF with the variable enclosed in single quotes to prepare for that (this is somewhat dirty solution as you have to treat the variable as an empty string and then convert it to integer):
INSERT INTO table(number1,number2,number3) VALUES (1,NULLIF('$var','')::integer,3);
Let x is a column of the table t allowing NULL values.
Which value should $value variable take for the following PDO statement to insert NULL value?
$db->prepare("INSERT t SET x=:x")->execute(array(':x'=>$value));
You simply insert it as null:
$db->prepare("INSERT t SET x=:x")->execute(array(':x'=>null));
If you want, you can also add the data type of PDO::PARAM_NULL.
This was asked 9 years ago, so it may no longer be relevant, but SeanWM's existing answer, while it works, does not technically answer specifically what was asked because it does not assign any value to a variable.
Just for completeness:
Inserting the PHP null value into MySQL does result in NULL being inserted into the DB.
$value = null;
$sql = $db->prepare("INSERT t SET x=:x")->execute(array(':x'=>$value));
Output:
Note: be careful NOT to enclose the word null in quotes or it will insert the string "null" instead.
My database contains empty table columns.
I would like to add a character like § to these empty rows so that I can search for them easier. How would I go about?
I already have a script that lets me replace or remove characters but I dont know a way to specify that rows that are empty should be updated with a character.
First, you probably don't have empty rows but empty column values in the rows. Wouldn't it be better if you just do it like if (!empty($row['column'])) instead of trying to put some bogus character?
Or if you want to do a SELECT just do something like this:
SELECT * FROM table_name WHERE column_name > ''; // seems to work for both NULL and empty string
Or:
DELETE FROM table_name WHERE column_name IS NULL or column_name = '';
UPDATE `table` SET column = "§" WHERE column = "";
It's bad to add character to an empty column because you are only adding extra size to the database. It's easy to search empty string on the database. Possible solutions of searching will be using of IS NULL to search for null columns.
SELECT * FROM tableName WHERE collName IS NULL
Another is by using CHAR_LENGTH (which gets the length of the data in the column)
SELECT * FROM tableName WHERE CHAR_LENGTH(collName) = 0
or by simply comparing it to ''
SELECT * FROM tableName WHERE colName = ''
I have a MYSQL table with an ENUM field named "offset" and some other columns. The field is defined as:
ENUM(0,1), can be NULL, predefined value NULL
Now I have two server. A production server and a development server and the same PHP script used to create and to update the database.
First step: the application create the record witout passing the "offset" in the CREATE query.
Second step: the application ask to the user some data (not the "offset" value), read the row inserted in step one and make an array, update some field (not the "offset" field), create a query in an automated fashion and save the row again with the updated values.
The automated query builder simple read all the field passed in an array and create the UPDATE string.
In both systems I obtain this array:
$values = array(... 'offset' => null);
and convert it in this same query passing the values in the mysql_real_escape_string:
UPDATE MyTable SET values..., `offset` = '' WHERE id = '10';
Now there is the problem. When i launch the query in the production system, the row is saved, in the development system I got an error and the db says that the offset data is wrong without saving the row.
From phpmyadmin when I create the row with the first step, it shows NULL in the offset field. After saving the field in the system which give no errors, it show me an empty string.
Both system are using MySQL 5 but the production uses 5.0.51 on Linux and development use 5.0.37 on Windows.
The questions:
Why one system give me an error an the other one save the field ? Is a configuration difference ?
Why when I save the field which is an enum "0" or "1" it saves "" and not NULL ?
Why one system give me an error an the other one save the field ? Is a configuration difference ?
Probably. See below.
Why when I save the field which is an enum "0" or "1" it saves "" and not NULL ?
According to the MySQL ENUM documentation:
The value may also be the empty string ('') or NULL under certain circumstances:
If you insert an invalid value into an ENUM (that is, a string not present in the list of permitted values), the empty string is inserted instead as a special error value. This string can be distinguished from a "normal" empty string by the fact that this string has the numeric value 0. ...
If strict SQL mode is enabled, attempts to insert invalid ENUM values result in an error.
(Emphasis added.)
strager's answer seems like a good explanation on why your code behaves differently on the 2 environments.
The problem lies elsewhere though. If you want to set a value to NULL in the query you shound use exactly NULL, but you are using mysql_real_escape_string() which result is always a string:
$ php -r 'var_dump(mysql_real_escape_string(null));'
string(0) ""
You should handle this differently. E.g:
$value = null
$escaped_value = is_null($value) ? "NULL" : mysql_real_escape_string($value);
var_dump($escaped_value);
// NULL
Some DB layers, like PDO, handle this just fine for you.
If you want it to be NULL, why don't you do this in the first place:
UPDATE MyTable SET values..., `offset` = NULL WHERE id = 10;