sql次級語句

來源:互聯網
上載者:User

標籤:

select upper(n_id) from nrc_news;
select left(n_content,1) from nrc_news;
select len(n_content) from nrc_news;

left() 返回字串左邊的字元
len() 返回字串的長度
lower() 轉換小寫
right() 返回字串右邊的字元
soundex() 返回字串的SOUNDEX值
upper() 轉換大寫

select n_content from nrc_news where DATEPART(YY,n_publishtime)=2015;
select n_content from nrc_news where DATEPART(YYYY,n_publishtime)=2015;
數值處理函數:
abs() 返回一個數的絕對值
cos() 返回一個角度的餘弦
exp() 返回一個數的指數值
pi() 返回圓周率
sin() 返回一個數的正弦
sqrt() 返回一個數的平方根
tan() 返回一個數的正切

select ABS(n_id) from nrc_news;
select cos(n_id) from nrc_news;
select exp(n_id) from nrc_news;
select pi() from nrc_news;
select SIN(n_id) from nrc_news;
select sqrt(n_id) from nrc_news;
select tan(n_id) from nrc_news;

摘要資料
AVG() 返回某列的平均值
COUNT() 返回某列的行數
MAX() 返回某列的最大值
MIN() 返回某列的最小值
SUM() 返回某列之和
select avg(t_id) from nrc_news;
select count(t_id) from nrc_news;
select max(t_id) from nrc_news;
select min(t_id) from nrc_news;
select sum(t_id) from nrc_news;
註:
avg()只能用來確定特定數值列的平均值,而且列明必須作為函數參數給出,如果要獲得多個列的平均值,必須用多個avg()
avg()函數忽略列值為NULL的行

select count(*) from nrc_news where t_id=10;

資料分組
select t_id,count(*)as number from nrc_news group by t_id;
註解:
上面的SELECT語句制定了兩個列,t_id和number(計算欄位).group by 子句會指示資料庫按t_id排序並分組資料.這樣會對每個t_id而不是整個表計算number
這樣輸出的就是每個t_id所對應的行的數量

select t_id,count(*)as number from nrc_news group by t_id having COUNT(*)>=2;
註:
HAVING 和 WHERE 的差別
where是在資料分組前進行過濾,having是在資料分組後進行過濾
共同使用的時候,where排除的值不包括在分組中,會影響having的計算

使用子查詢
select * from nrc_news where t_id in(1,2);
select t_id from nrc_news where n_content like ‘%就%‘;

select * from nrc_news where t_id in(select t_id from nrc_news where n_content like ‘%就%‘);
註:
作為子查詢的select語句只能查詢單個列,企圖檢索多個列將返回錯誤
select r_id,r_content,n_id,(select t_id from nrc_news where nrc_news.n_id=nrc_review.n_id)as orders from nrc_review ;
select n_id,n_title,t_id,(select t_memo from nrc_type where nrc_type.t_id=nrc_news.t_id)as t_memo from nrc_news
上述子查詢為重點內容。重點記憶。
select top 10 n_id as id,
(select n_title from nrc_news where nrc_news.n_id=nrc_review.n_id)as title,
(select n_content from nrc_news where nrc_news.n_id=nrc_review.n_id)as content,
(select t_id from nrc_news where nrc_news.n_id=nrc_review.n_id)as t_id,
(select n_publishtime from nrc_news where nrc_news.n_id=nrc_review.n_id)as publishtime,
COUNT(*)as number from nrc_review group by n_id order by number desc,id;

sql次級語句

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.