今日份python 表單查詢
有乙個叫orm模式的,會顯示sql底層**
當我們用python去查詢是,再用showsql就可以看到剛才查詢到的底層sql語句
在pycharm有個pythonconsole
顯示orm底層生成sql
def showsql():查詢單個物件from django.db import connection
queries = connection.queries
print queries[-1]['sql']
#所有的繼承自models.model的類 都會有乙個叫objects(管理器)
* 查詢乙個 (有且只能有乙個)(少於乙個或者多餘乙個都報錯)
* 返回值movie物件
movie.objects.get(mid=147)
select `movie`.`mid`, `movie`.`mname`, `movie`.`mdesc`, `movie`.`mimg`, `movie`.`mlink` from `movie` where `movie`.`mid` = 147
* 獲得第乙個
movie.objects.first()
select `movie`.`mid`, `movie`.`mname`, `movie`.`mdesc`, `movie`.`mimg`, `movie`.`mlink` from `movie` order by `movie`.`mid` asc limit 1
* 獲得最後乙個
movie.objects.last()
select `movie`.`mid`, `movie`.`mname`, `movie`.`mdesc`, `movie`.`mimg`, `movie`.`mlink` from `movie` order by `movie`.`mid` desc limit 1
* 獲得記錄的總數
movie.objects.count()
select count(*) as `__count` from `movie`
查詢多個物件
* 獲得所有的記錄movie.objects.all()
# 在diango的1.11.6中預設只是獲得21個,
# 懶載入
select `movie`.`mid`, `movie`.`mname`, `movie`.`mdesc`, `movie`.`mimg`, `movie`.`mlink` from `movie` limit 21
* 切片 (不支援負數索引)
movie.objects.all()[20:40]
# 底層直接使用了limit字句,可以自動的分頁
# django的orm效能非常強大,能節省很多任務作
select `movie`.`mid`, `movie`.`mname`, `movie`.`mdesc`, `movie`.`mimg`, `movie`.`mlink` from `movie` limit 20 offset 20
* 過濾
# 集和(游標,結果集,查詢集)
movie.objects.filter(mname='麻辣學院')
select `movie`.`mid`, `movie`.`mname`, `movie`.`mdesc`, `movie`.`mimg`, `movie`.`mlink` from `movie` where `movie`.`mname` = '麻辣學院' limit 21
* 模糊查詢
* like
* %(多個字元) _(乙個字元)
* select * from movie where mname like '%愛情_'
* 查詢愛情結尾的
select * from movie where mname like '%愛情'
movie.objects.filter(mname__endswith='愛情')
select `movie`.`mid`, `movie`.`mname`, `movie`.`mdesc`, `movie`.`mimg`, `movie`.`mlink` from `movie` where `movie`.`mname` like binary '%愛情' limit 21
* 查詢愛情開頭的
select * from movie where mname like '愛情%'
movie.objects.filter(mname__startswith='愛情')
select `movie`.`mid`, `movie`.`mname`, `movie`.`mdesc`, `movie`.`mimg`, `movie`.`mlink` from `movie` where `movie`.`mname` like binary '愛情%' limit 21
* 包含愛情的
select * from movie where mname like '%愛情%'
movie.objects.filter(mname__contains='愛情')
select `movie`.`mid`, `movie`.`mname`, `movie`.`mdesc`, `movie`.`mimg`, `movie`.`mlink` from `movie` where `movie`.`mname` like binary '%愛情%' limit 21
* 完全相等
movie.objects.filter(mname__exact='麻辣學院')
movie.objects.filter(mname='麻辣學院')
select `movie`.`mid`, `movie`.`mname`, `movie`.`mdesc`, `movie`.`mimg`, `movie`.`mlink` from `movie` where `movie`.`mname` = '麻辣學院' limit 21
* 忽略大小寫
movie.objects.filter(mname__istartswith='h')
* 查詢某個字段是否為null
movie.objects.filter(mname__isnull=true)
* 多條件查詢
movie.objects.filter(mname__contains='愛情',mid=147)
and
select `movie`.`mid`, `movie`.`mname`, `movie`.`mdesc`, `movie`.`mimg`, `movie`.`mlink` from `movie` where (`movie`.`mname` like binary '%愛情%' and `movie`.`mid` = 147) limit 21
movie.objects.filter(mname__contains='愛情').filter(mid=147)
* 部分查詢
movie.objects.values('mname','mid').filter(mname__contains='愛情')
* 排除一部分
movie.objects.filter(mname__contains='愛情').exclude(mname__startswith='愛情')
select `movie`.`mid`, `movie`.`mname`, `movie`.`mdesc`, `movie`.`mimg`, `movie`.`mlink` from `movie` where (`movie`.`mname` like binary '%愛情%' and not (`movie`.`mname` like binary '愛情%')) limit 21
* 排序
movie.objects.order_by('mid')
select `movie`.`mid`, `movie`.`mname`, `movie`.`mdesc`, `movie`.`mimg`, `movie`.`mlink` from `movie` order by `movie`.`mid` asc limit 21
movie.objects.order_by('-mid')
select `movie`.`mid`, `movie`.`mname`, `movie`.`mdesc`, `movie`.`mimg`, `movie`.`mlink` from `movie` order by `movie`.`mid` desc limit 21
mysql表單查詢 表單查詢例項
toc 1.查詢部門30中員工的詳細資訊。select from emp where deptno 30 2.找出從事clerk工作的員工的編號 姓名 部門號。select empno,ename,deptno from emp where job clerk 3.檢索出獎金多於基本工資的員工資訊。...
mysql表單查詢
以下是練習題 create table student id int,name varchar 20 chinese float,english float,math float desc student insert into student id,name,chinese,english,mat...
c 第二次作業 2019 03 21
1.給定兩個整形變數的值,將兩個值的內容進行交換.include include int main 2.不允許建立臨時變數,交換兩個數的內容.include include int main for i 0 isystem pause return 0 5.求十個整數中最大值.define crt ...