最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

MySQLNULL值处理实例教程

来源:动视网 责编:小采 时间:2020-11-09 09:00:30
文档

MySQLNULL值处理实例教程

MySQLNULL值处理实例教程:MySQL NULL值处理我们已经知道MySQL使用SQL SELECT命令和WHERE子句来读取数据表中的数据,但是当提供的查询条件字段为NULL时,该命令可能就无法正常工作。为了处理这种情况时,MySQL提供了三大运算符:IS NULL:当列的值为NULL,此运算符返回true。I
推荐度:
导读MySQLNULL值处理实例教程:MySQL NULL值处理我们已经知道MySQL使用SQL SELECT命令和WHERE子句来读取数据表中的数据,但是当提供的查询条件字段为NULL时,该命令可能就无法正常工作。为了处理这种情况时,MySQL提供了三大运算符:IS NULL:当列的值为NULL,此运算符返回true。I


MySQL NULL值处理

我们已经知道MySQL使用SQL SELECT命令和WHERE子句来读取数据表中的数据,但是当提供的查询条件字段为NULL时,该命令可能就无法正常工作。

为了处理这种情况时,MySQL提供了三大运算符:

IS NULL:当列的值为NULL,此运算符返回true。

IS NOT NULL:当列的值不为NULL,运算符返回true。

<=>: 比较操作符(不同于=运算符),当比较的的两个值为NULL时返回真。

关于NULL的条件比较运算是比较特殊的。你不能使用= NULL或!= NULL在列中查找NULL值。

在MySQL中,NULL值与任何其它值的比较(即使是NULL)永远返回false,即NULL = NULL返回false。

MySQL中处理NULL使用IS NULL和IS NOT NULL运算符。

在命令提示符中使用NULL值

以下实例中假设数据库指南中的表tcount_tbl包含两列tutorial_author和tutorial_count,tutorial_count中设置插入NULL值。

尝试以下实例:

root @ host#mysql -u root -p password;
输入密码:*******
mysql> use TUTORIALS;数据库已更改mysql> create table tcount_tbl
 - >(
 - > tutorial_author varchar(40)NOT NULL,
 - > tutorial_count INT
 - >);
查询OK,0行受影响(0.05秒)
mysql> INSERT INTO tcount_tbl
 - >(tutorial_author,tutorial_count)值('mahran',20);
mysql> INSERT INTO tcount_tbl
 - >(tutorial_author,tutorial_count)values('mahnaz',NULL);
mysql> INSERT INTO tcount_tbl
 - >(tutorial_author,tutorial_count)值('Jen',NULL);
mysql> INSERT INTO tcount_tbl
 - >(tutorial_author,tutorial_count)值('Gill',20);
mysql> select * from tcount_tbl;
+ ----------------- + ---------------- +
| tutorial_author | tutorial_count |
+ ----------------- + ---------------- +
| 马赫兰 20 |
| mahnaz | NULL |
| 仁| NULL |
| 鳃| 20 |
+ ----------------- + ---------------- +
4行(0.00秒)
MySQL的>

以下实例中你可以看到=和!=运算符是不起作用的

mysql> SELECT * FROM tcount_tbl WHERE tutorial_count = NULL;
空置(0.00秒)
mysql> SELECT * FROM tcount_tbl WHERE tutorial_count!= NULL;
空置(0.01秒)

查询数据表中tutorial_count列是否为NULL,必须使用IS NULL和IS NOT NULL,如下实例:

mysql> SELECT * FROM tcount_tbl 
 - > WHERE tutorial_count IS NULL;
+ ----------------- + ---------------- +
| tutorial_author | tutorial_count |
+ ----------------- + ---------------- +
| mahnaz | NULL |
| 仁| NULL |
+ ----------------- + ---------------- +
2行(0.00秒)
mysql> select * from tcount_tbl 
 - > WHERE tutorial_count is NOT NULL;
+ ----------------- + ---------------- +
| tutorial_author | tutorial_count |
+ ----------------- + ---------------- +
| 马赫兰 20 |
| 鳃| 20 |
+ ----------------- + ---------------- +
2行(0.00秒)

使用PHP脚本处理NULL值

PHP脚本中你可以在if ... else语句来处理变量是否为空,并生成相应的条件语句。

以下实例中PHP设置了$ tutorial_count变量,然后使用该变量与数据表中的tutorial_count字段进行比较:

<?PHP
$ dbhost ='localhost:3036';
$ dbuser ='root';
$ dbpass ='rootpassword';
$ conn = mysql_connect($ dbhost,$ dbuser,$ dbpass);
if(!$ conn)
{
 die('无法连接:'。mysql_error());
}
if(isset($ tutorial_count))
{
 $ sql ='SELECT tutorial_author,tutorial_count
 FROM tcount_tbl
 WHERE tutorial_count = $ tutorial_count';
}
其他
{
 $ sql ='SELECT tutorial_author,tutorial_count
 FROM tcount_tbl
 WHERE tutorial_count IS $ tutorial_count';
}
mysql_select_db( '教程');
$ retval = mysql_query($ sql,$ conn);
如果(!$ retval)
{
 die('无法获取数据:'mysql_error());
}
while($ row = mysql_fetch_array($ retval,MYSQL_ASSOC))
{
 echo“作者:{$ row ['tutorial_author']} <br>”。
 “Count:{$ row ['tutorial_count']} <br>”。
 “--------------------------------结果”;
} 
echo“成功获取数据\ n”;
mysql_close($康恩);
?>

【相关推荐】

1. 特别推荐:“php程序员工具箱”V0.1版本下载

2. 免费mysql在线视频教程

3. 数据库设计那些事

文档

MySQLNULL值处理实例教程

MySQLNULL值处理实例教程:MySQL NULL值处理我们已经知道MySQL使用SQL SELECT命令和WHERE子句来读取数据表中的数据,但是当提供的查询条件字段为NULL时,该命令可能就无法正常工作。为了处理这种情况时,MySQL提供了三大运算符:IS NULL:当列的值为NULL,此运算符返回true。I
推荐度:
标签: 处理 教程 实例
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top