Inserting NULL value to MySQL, from Coldfusion
I have a database that may have a value in one of the fields. Circum开发者_如何学Pythonstance may need to remove the initial value and replace it with a NULL.
How can NULL values be inserted in an empty field?
Appreciate it.
You'd write a query that's something like this:
<cfquery name="queryName" datasource="dsn">
UPDATE tableName SET fieldName = NULL
WHERE recordId = <cfqueryparam cfsqltype="cf_sql_integer" value="#recordId#" />
</cfquery>
Also, there's a difference between a blank string and a NULL value. The thing to remember about NULL is that it doesn't equate to anything but NULL. But it also doesn't equate to non-NULL values. So if you have three records in your table:
ID VALUE
1 15
2 NULL
3 30
When you run this query:
SELECT ID from tableName where value != 15
you will ONLY get record 3. The way to make sure record 2 is included in the resultset is to alter your query as such:
SELECT ID from tableName where coalesce(value,16) != 15
The purpose of coalesce is to check the first parameter for NULL-ness, and use the value of the second parameter instead if the first parameter is NULL. In the above example, I've put '16' as the second parameter, but you can use any value you want as long as it's not the same as what you're comparing for (in this case, 15).
精彩评论