SQL语句笔记1

1.连接数据库.在Juptor中进行SQL操作时,需要加上%sql,%%sql

%sql mysql://studentuser:studentpw@mysqlserver/dognitiondb
/*Now that the SQL library is loaded, we need to connect to a 
database. The following command will log you into the MySQL 
server at mysqlserver as the user 'studentuser' and will select the 
database named 'dognitiondb' :
*/

2.查看数据库中的表格

/*. To determine how many tables each database has, use the SHOW command:
*/

%sql SHOW tables 
SHOW columns FROM (enter table name here)

/*or if you have multiple databases loaded**/
SHOW columns FROM (enter table name here) FROM (enter database name here)

/*or*/
SHOW columns FROM databasename.tablename

或者用DESCRIBE函数代替。

/*DESCRIBE tablename*/

%sql DESCRIBE reviews

3.SELECT语法

/*Using SELECT to look at your raw data*/
/*
SELECT is used anytime you want to retrieve data from a table. In order to retrieve that data, you always have to provide at least two pieces of information:
(1) what you want to select, and      
(2) from where you want to select it.  
Table or column names with spaces in them need to be surrounded by quotation marks in SQL. MySQL accepts both double and single quotation marks,
but some database systems only accept single quotation marks.
In all database systems, if a table or column name contains an SQL keyword, the name must be enclosed in backticks instead of quotation marks.
*/ %%sql SELECT breed FROM dogs;

4.LIMIT

/*
In the next cell, try entering a query that will let you see the first 10 rows of the breed column in the dogs table.
*/

%%sql 
SELECT breed
FROM dogs LIMIT 10

5.OFFSET

SELECT breed
FROM dogs LIMIT 10 OFFSET 5;
/*10 rows of data will be returned, starting at Row 6.
or */
SELECT breed
FROM dogs LIMIT 5, 10;

6.稍微复杂点的

/*从表中选择多列
*/
SELECT breed, breed_type, breed_group
FROM dogs LIMIT 5, 10;
/*return all the data in a table.(wild card)*/
SELECT *
FROM dogs LIMIT 5, 10;

算术操作。

/*
SELECT statements can also be used to make new derivations of individual columns using "+" for addition, "-" for subtraction, "*" for multiplication, or "/" for division. For example, if you wanted the median inter-test intervals in hours instead of minutes or days, you could query:
*/
SELECT median_iti_minutes / 60
FROM dogs LIMIT 5, 10;

猜你喜欢

转载自www.cnblogs.com/Shinered/p/9501070.html
今日推荐