sql语句是专门与数据库交互的语言,支持多种DBMS,不区分大小写!
一、检索语句
1.检索单列所有行数据:select user_name from users;
2.检索多个列所有数据:select user_id,user_name,user_sex from users;
3.检索所有列:select * from users;
4.检索不同的值:select distinct user_name from users;
5.检索指定前5行的数据:
①sqlServer/Access:select top 5 user_name from users;
②DB2:select user_name from users fetch first 5 rows only;
③Oracle: select user_name from users where rownum <= 5;
④Mysql:select user_name from users limit 5;
1>返回从第五行起的5行数据:select user_name from users limit 5 offset 5;
6.排序语句:select user_age from users order by user_age;
①使用order by 语句是,必须保证其是sql语句中的最后一条字句
②对多列进行排序:select user_age,user_name from users order by user_age desc,user_name;(user_age倒序,user_age正序)
7.where语句:select user_name from users where user_age = 18;
①范围查询:select user_name from users where user_age between 18 and 20;
②空值查询:select user_name from users where user_age is null;
③组合查询:select user_name from users where user_age = 18 and user_sex = '男';
④求值查询:select user_name from users where (user_age = 18 or user_age = 20) and user_sex = '男'
1>在sql语句中,连接条件的执行顺序为:()> and > or
2>上述语句还可改为:select user_name from users where user_age in (18,20) and user_sex = '男'
8.通配符过滤查询:select * from users where user_name like '张%';(查询所有姓名为张开头的所有用户信息)