解法練習
案例1.子查詢練習
字段 說明
film_id 電影id
title 電影名稱
description 電影描述資訊
category_id 電影分類id
name 電影分類名稱
last_update 電影分類最後更新時間
film_id 電影id
category_id 電影分類id
last_update 電影id和分類id對應關係的最後更新時間
ilm表
create table if not exists film (category表film_id smallint(5) not null default '0',
title varchar(255) not null,
description text,
primary key (film_id));
create table category (film_category表category_id tinyint(3) not null ,
name varchar(25) not null, `last_update` timestamp,
primary key ( category_id ));
create table film_category (film_id smallint(5) not null,
category_id tinyint(3) not null, `last_update` timestamp);
使用子查詢的方式找出屬於action分類的所有電影對應的title,description
解題命令
selecttitle,description
from
film
where film_id in(select film_id from
film_category
where category_id in(select category_id from
category w
where name=
'action
'))
--子查詢解法
select
f.title,f.description
from film as f inner
join film_category as fc on f.film_id =
fc.film_id
inner
join category as c on c.category_id =
fc.category_id
where c.name =
'action';
--常規解法
名詞解釋補充
explain解釋
獲取select * from employees對應的執行計畫
explain select*from employees
連線字串
將employees表的所有員工的last_name和first_name拼接起來作為name,中間以乙個空格區分
--mysql、sql server、oracle等資料庫支援concat方法,而本題所用的sqlite資料庫只支援用連線符號"
||"來連線字串
--concat方法:
select concat(concat(last_name," "),first_name) as name from
employees
--或者
select concat(last_name," ",first_name) as name from
employees
--本題中使用:
select last_name||" "||first_name as name from employees
插入資料時,已有則忽略
insert ignore into『表』values()
建立索引
createunique
index ... on
... 建立唯一索引值
create
index ... on
... 建立普通索引值--例
create
unique
index uniq_idx_firstname on
actor(first_name);
create
index idx_lastname on actor(last_name);
**:擷取字串
substr(字串,起始位置,長度)起始位置:擷取的子串的起始位置(注意:字串的第乙個字元的索引是1)。值為正時從字串開始位置 開始計數,值為負時從字串結尾位置開始計數。
長度:擷取子串的長度
--例 取first_name最後兩位
select first_name from
employees
order
by substr(first_name,length(first_name)-
1,2)
--mysql中的right函式
select first_name from employees order by right(first_name ,2);
group_concat() 函式
group_concat( [distinct
] 要連線的字段 [
order by 排序字段 asc/desc][
separator 『分隔符』
]--分組後連線
PTA練習隨筆
本文是做題過程中學習到的一些經驗總結 1.對兩個整數做除法想得到浮點數時,可以對任意乙個變數先做乘法,乘1.0,但是要注意一定是先乘1.0再做除法,否則就會出錯 正確 printf d d 2f a,b,a b 1.0 錯誤 printf d d 2f a,b,a b 1.0 由於除數乘1.0還需要...
SQL練習隨筆2
sql練習 2 查詢倒數第3個入職的人員資訊 牛客網sql 我的思路 對hire date進行倒排 取第三個 select from employees order by hire date desc limit 2,1 考慮到可能有多個員工資訊,嚴謹一些可以寫為 嚴謹的寫法 select from...
MySQL索引隨筆
1.mysql索引分主鍵索引 非主鍵索引,非主鍵索引一般也叫二級索引。非主鍵索引可以分為唯一索引與普通索引 mysql索引都以b 樹的形式儲存 平衡多叉樹 以 innodb 的乙個整數字段索引為例,這個 n 差不多是1200。這棵樹高是 4 的時候,就可以存 1200 的 3 次方個值,這已經 17...