利用SQL语句查找姓“张”且分数大于80的记录(某公司招聘笔试试题)

来源:互联网 发布:多仓库贪心法算法 编辑:程序博客网 时间:2024/05/02 10:59

        某公司招聘的笔试试题中有这样一个题目:利用SQL语句查找姓“张”且分数大于80的记录。 其实, 这个题目很简单, 主要考察求职者有没有用过SQL, 下面, 我们来一起实战一下。


        关于SQL练习环境的搭建, 其实非常简单(比如在Windows上, 你只需要下载一个sqlite3.exe即可), 只需要参考我之前的博文即可:“为想学SQLite或练习SQL语言的朋友搭建简单的命令行环境------在Windows, Linux, Android(用adb连接安卓手机)上玩转SQLite数据库的sqlite3命令行”。地址:http://blog.csdn.net/stpeace/article/details/45652325

        

        下面, 我们以Windows为例, 将如下命令一起复制到cmd中(注意: 有的朋友反馈说, rm my.db后没有效果, 并不能删除数据库文件。 这是因为, 你的Windows上没有安装这个命令, 而我的Windows上安装了这个命令。 其实, 安装没安装, 都不要太较劲, 没啥影响的):

sqlite3 my.dbcreate table test(name PRIMARY KEY, age int, score int);insert into test values('Eric', 27, 90);insert into test values('张三丰', 100, 85);insert into test values('张三疯', 25, 70);insert into test values('比尔盖茨', 50, 95);select * from test;select * from test where name like '张%' and score > 80;.exitrm my.dbxxxxxxxxxxxxxxxxxxxxxxxxxxxx

        得到的结果是:

C:\Documents and Settings\Administrator\桌面\sql>C:\Documents and Settings\Administrator\桌面\sql>sqlite3 my.dbSQLite version 3.8.5 2014-06-04 14:06:34Enter ".help" for usage hints.sqlite> create table test(name PRIMARY KEY, age int, score int);sqlite> insert into test values('Eric', 27, 90);sqlite> insert into test values('张三丰', 100, 85);sqlite> insert into test values('张三疯', 25, 70);sqlite> insert into test values('比尔盖茨', 50, 95);sqlite> select * from test;Eric|27|90张三丰|100|85张三疯|25|70比尔盖茨|50|95sqlite> select * from test where name like '张%' and score > 80;张三丰|100|85sqlite> .exitC:\Documents and Settings\Administrator\桌面\sql>rm my.dbC:\Documents and Settings\Administrator\桌面\sql>

       其核心语句是:select * from test where name like '张%' and score > 80;  而这正是笔试题目的答案。其中, %可以与任意零个或者多个字符匹配, 这个百分号是贪婪匹配。


       SQL语句必须熟练使用, 要多多联系, 熟能生巧, 后续我们会更多地进行介绍, 本文就先写到这里了。







0 0