1、关于 having:
例1:
在开发时,我们经常会遇到以“累计(count)”或是“累加(sum)”为条件的查询。比如user_num表:
id user num
1 a 3
2 a 4
3 b 5
4 b 7
select user from user_num group by user having count(user)>=2
查询出现过2次的user。 往往初学者会错误地认为在where 语句里直接使用count()算法,很显然这个想法是错误的,count()方法并不能被用在where子句中,
为了解决问题,我们可以在group by子句后面使用HAVING来做条件限制。
错误做法:select * from user_num where count(user)>=2 group by user;
正确做法:select * from user_num group by user HAVING count(user)>=2 ; 解释说明:[color=brown]HAVING 与 WHERE 类似,可用来决定选择哪些记录。
HAVING 子句在SELECT语句中指定,显示哪些已用 GROUP BY 子句分组的记录。在GROUP BY组合了记录后, HAVING会显示 GROUP BY 子句分组的任何符合 HAVING
子句的记录。 [/color]
2、 select * from user where nick_name is not null;
3、 select * from user where status in (0,1);
select * from user where id > 100 and (status=0 or status =1);
4、set names 'utf8';
5、
create database zoneidc; drop database [数据库名];
insert into comic_pic (vol_id,file_path,order_num,status) VALUES (4,'haha',0,0),(4,'haha',0,0),(6,'kiki',0,0) //插入多行数据
mysql -u root --default-character-set=utf8 -p db_choose < C:\db_choose.sql
mysqldump -u root -p --default-character-set=utf8 db_choose > C:\db_choose.sql
select * from log_index where date_format(submit_date,'%Y-%m-%d')='2006-01-08'
grant select,insert,update,delete,create,drop on vtdc.employee to joe@10.163.225.87 identified by '123';
grant all privileges on v133_upload.* to v_upload@'%' identified by 'you2top';
grant all privileges on v133_upload.* to v_upload@'%' identified by 'you2top';
flush privileges
revoke all privileges on *.* from v_upload@'%';//删除 用户‘V_UPLOAD’的所有权限
delete from user where user='v_upload' and host='%'//删除用户
flush privileges
mysql -uroot -p
use mysql
select * from user
update user set password=old_password('tobey130982') where user.user = 'zhangyinpei' limit 1;
select * from user
flush privileges
/home/mysql5.0/bin/mysqld_safe --user=mysql &//启动mysql
例1:
在开发时,我们经常会遇到以“累计(count)”或是“累加(sum)”为条件的查询。比如user_num表:
id user num
1 a 3
2 a 4
3 b 5
4 b 7
select user from user_num group by user having count(user)>=2
查询出现过2次的user。 往往初学者会错误地认为在where 语句里直接使用count()算法,很显然这个想法是错误的,count()方法并不能被用在where子句中,
为了解决问题,我们可以在group by子句后面使用HAVING来做条件限制。
错误做法:select * from user_num where count(user)>=2 group by user;
正确做法:select * from user_num group by user HAVING count(user)>=2 ; 解释说明:[color=brown]HAVING 与 WHERE 类似,可用来决定选择哪些记录。
HAVING 子句在SELECT语句中指定,显示哪些已用 GROUP BY 子句分组的记录。在GROUP BY组合了记录后, HAVING会显示 GROUP BY 子句分组的任何符合 HAVING
子句的记录。 [/color]
2、 select * from user where nick_name is not null;
3、 select * from user where status in (0,1);
select * from user where id > 100 and (status=0 or status =1);
4、set names 'utf8';
5、
create database zoneidc; drop database [数据库名];
insert into comic_pic (vol_id,file_path,order_num,status) VALUES (4,'haha',0,0),(4,'haha',0,0),(6,'kiki',0,0) //插入多行数据
mysql -u root --default-character-set=utf8 -p db_choose < C:\db_choose.sql
mysqldump -u root -p --default-character-set=utf8 db_choose > C:\db_choose.sql
select * from log_index where date_format(submit_date,'%Y-%m-%d')='2006-01-08'
grant select,insert,update,delete,create,drop on vtdc.employee to joe@10.163.225.87 identified by '123';
grant all privileges on v133_upload.* to v_upload@'%' identified by 'you2top';
grant all privileges on v133_upload.* to v_upload@'%' identified by 'you2top';
flush privileges
revoke all privileges on *.* from v_upload@'%';//删除 用户‘V_UPLOAD’的所有权限
delete from user where user='v_upload' and host='%'//删除用户
flush privileges
mysql -uroot -p
use mysql
select * from user
update user set password=old_password('tobey130982') where user.user = 'zhangyinpei' limit 1;
select * from user
flush privileges
/home/mysql5.0/bin/mysqld_safe --user=mysql &//启动mysql