1. 程式人生 > >SQL語句筆記1

SQL語句筆記1

back sta owin nts car user data rst ring

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;

SQL語句筆記1