博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
creating a table and inserting data
阅读量:4364 次
发布时间:2019-06-07

本文共 2689 字,大约阅读时间需要 8 分钟。

/** Grocery list: Bananas (4)Peanut Butter (1)Dark Chocolate Bars (2)**/CREATE TABLE groceries (id INTEGER PRIMARY KEY, name TEXT, quantity INTEGER );INSERT INTO groceries VALUES (1, "Bananas", 4);INSERT INTO groceries VALUES (2, "Peanut Butter", 1);INSERT INTO groceries VALUES (3, "Dark chocolate bars", 2);SELECT * FROM groceries;

 

This database contains an incomplete list of box office hits and their release year. In this challenge, you're going to get the results back out of the database in different ways! In this first step, just select all the movies.

Now, add a second query after the first, that retrieves only the movies that were released in the year 2000 or later, not before. Sort the results so that the earlier movies are listed first. You should have 2 SELECT statements after this step.

 

CREATE TABLE movies (id INTEGER PRIMARY KEY, name TEXT, release_year INTEGER);INSERT INTO movies VALUES (1, "Avatar", 2009);INSERT INTO movies VALUES (2, "Titanic", 1997);INSERT INTO movies VALUES (3, "Star Wars: Episode IV - A New Hope", 1977);INSERT INTO movies VALUES (4, "Shrek 2", 2004);INSERT INTO movies VALUES (5, "The Lion King", 1994);INSERT INTO movies VALUES (6, "Disney's Up", 2009); SELECT * FROM movies;SELECT * FROM movies WHERE release_year >= 2000 ORDER BY release_year;

The default sequence is ASC(ascending)order, so from small at the top to large at the bottom.

  • Aggregating date

CREATE TABLE groceries (id INTEGER PRIMARY KEY, name TEXT, quantity INTEGER, aisle INTEGER);INSERT INTO groceries VALUES (1, "Bananas", 56, 7);INSERT INTO groceries VALUES(2, "Peanut Butter", 1, 2);INSERT INTO groceries VALUES(3, "Dark Chocolate Bars", 2, 2);INSERT INTO groceries VALUES(4, "Ice cream", 1, 12);INSERT INTO groceries VALUES(5, "Cherries", 6, 2);INSERT INTO groceries VALUES(6, "Chocolate syrup", 1, 4);SELECT aisle, SUM(quantity) FROM groceries GROUP BY aisle;

 

GROUP BY表示从aisle挑选出元素进行汇总

 

  • More complex queries with AND/OR

CREATE TABLE exercise_logs    (id INTEGER PRIMARY KEY AUTOINCREMENT,    /*id 序号自动增加*/    type TEXT,    minutes INTEGER,     calories INTEGER,    heart_rate INTEGER);INSERT INTO exercise_logs(type, minutes, calories, heart_rate) VALUES ("biking", 30, 100, 110);    /*重新声明,只填需要填充的元素名称*/INSERT INTO exercise_logs(type, minutes, calories, heart_rate) VALUES ("biking", 10, 30, 105);INSERT INTO exercise_logs(type, minutes, calories, heart_rate) VALUES ("dancing", 15, 200, 120);SELECT * FROM exercise_logs WHERE calories > 50 ORDER BY calories;/* AND */SELECT * FROM exercise_logs WHERE calories > 50 AND minutes < 30;/* OR */SELECT * FROM exercise_logs WHERE calories > 50 OR heart_rate > 100;

 

 

 

 

 

转载于:https://www.cnblogs.com/ruruozhenhao/p/8658319.html

你可能感兴趣的文章
Python进阶06 循环对象
查看>>
Python补充06 Python之道
查看>>
.net压缩文件夹
查看>>
MS SQL 分页存储过程
查看>>
2016年,我的和自己谈谈
查看>>
创建一个自己的GitHub,创建自己的开源项目
查看>>
Topcoder SRM 648 (div.2)
查看>>
Constructor总结
查看>>
HttpClient使用之下载远程服务器中的文件(注意目录遍历漏洞)
查看>>
JAVA UDP网络编程学习笔记
查看>>
反素数 -- 数学
查看>>
CODEVS 1205 单词反转
查看>>
洛谷 P3367 【模板】并查集
查看>>
求质数算法的N种境界 (N > 10) zz
查看>>
XmlPullParserException
查看>>
机器学习降维算法一:PCA(主成分分析算法)
查看>>
第五周总结
查看>>
Beam概念学习系列之Pipeline Runners
查看>>
Elasticsearch之需要注意的问题(es和jdk版本)
查看>>
HBASE启动失败,Failed construction of Master: class org.apache.hadoop.hbase.master.HMaster
查看>>