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

SQL語句筆記3

exe res *** ica where pos mar creat 默認

參考鏈接:

http://dev.mysql.com/doc/refman/5.7/en/func-op-summary-ref.html
http://www.w3resource.com/mysql/mysql-functions-and-operators.php

1. SQL 改變列名。

/*if you wanted to change the name of the time stamp field of the completed_tests table from "created_at" to "time_stamp" in your output, you could take advantage of the AS clause and execute the following query:
*/ SELECT dog_guid, created_at AS time_stamp FROM complete_tests /********************* Note that if you use an alias that includes a space, the alias must be surrounded in quotes: */ SELECT dog_guid, created_at AS "time stamp" FROM complete_tests /***************/ /* You could also make an alias for a table:
*/ SELECT dog_guid, created_at AS "time stamp" FROM complete_tests AS tests

2. 選擇列中不同的值

SELECT DISTINCT breed
FROM dogs;
/************************/
/*
ll the possible combinations of states and cities in the users table, you could query.
*/
SELECT DISTINCT state, city
FROM users;
/***************************
*/ /* When you use the DISTINCT clause with the LIMIT clause in a statement, MySQL stops searching when it finds the number of unique rows specified in the LIMIT clause. */ SELECT DISTINCT breed FROM dogs LIMIT 5;

3. ORDER BY 排序

/*********************/
/*
If you wanted the breeds of dogs in the dog table sorted in alphabetical order, you could query.
*/
SELECT DISTINCT breed
FROM dogs 
ORDER BY breed
/************默認為升序,也可以降序*****************/
/*
The default is to sort the output in ascending order. However, you can tell SQL to sort the output in descending order as well:
*/
SELECT DISTINCT breed
FROM dogs 
ORDER BY breed DESC

/*
 you could select the User IDs and Dog IDs of the 5 customer-dog pairs who spent the least median amount of time between their Dognition tests:
*/
SELECT DISTINCT user_guid, median_ITI_minutes
FROM dogs 
ORDER BY median_ITI_minutes
LIMIT 5
/***************************/
/*
or the greatest median amount of time between their Dognition tests:
*/
SELECT DISTINCT user_guid, median_ITI_minutes
FROM dogs 
ORDER BY median_ITI_minutes DESC
LIMIT 5
/******************************/
/*
You can also sort your output based on a derived field
*/
SELECT DISTINCT user_guid, (median_ITI_minutes * 60) AS median_ITI_sec
FROM dogs 
ORDER BY median_ITI_sec DESC
LIMIT 5

根據多列排序

/*
If you wanted to select all the distinct User IDs of customers in the United States (abbreviated "US") and sort them according to the states they live in in alphabetical order first, and membership type second, you could query:
*/
SELECT DISTINCT user_guid, state, membership_type
FROM users
WHERE country="US"
ORDER BY state ASC, membership_type ASC

/*
You might notice that some of the rows have null values in the state field. You could revise your query to only select rows that do not have null values in either the state or membership_type column:
*/
SELECT DISTINCT user_guid, state, membership_type
FROM users
WHERE country="US" AND state IS NOT NULL and membership_type IS NOT NULL
ORDER BY state ASC, membership_type ASC

4.輸出文件

/*
To tell MySQL to put the results of a query into a variable, use the following syntax:
variable_name_of_your_choice = %sql [your full query goes here];

the_output_name_you_want.csv(‘the_output_name_you_want.csv‘)
*/
breed_list = %sql SELECT DISTINCT breed FROM dogs ORDER BY breed;

breed_list.csv(breed_list.csv)

5.字符串處理

/*
"REPLACE(str,from_str,to_str)
Returns the string str with all occurrences of the string from_str replaced by the string to_str. REPLACE() performs a case-sensitive match when searching for from_str."
*/
SELECT DISTINCT breed,
REPLACE(breed,-,‘‘) AS breed_fixed
FROM dogs
ORDER BY breed_fixed

/********TRIM functions*********/
SELECT DISTINCT breed, TRIM(LEADING - FROM breed) AS breed_fixed
FROM dogs
ORDER BY breed_fixed

SQL語句筆記3