SQL 簡單的查詢常用套路

2021-09-02 03:46:37 字數 426 閱讀 6026

1.在查詢的時候,如果是有兩個並且的條件的時候,比如:查詢同時參加語文考試和英語考試的人資訊,這個人要同時參加了兩門學科的查詢,這裡不能直接使用and來做拼接,如果直接是 學科="語文"  and 學科="英語",表現出來的效果是在這一條記錄中此字段要既是語文又是英語,這是不可能的,是查詢不到資料的,

但是並且又只有and,這裡有乙個小方法,就是將查詢條件分開處理。第一階段查詢用  in  關鍵字先查找到第乙個條件,然後在使用 and 查詢第二個條件。比如: select *  from stu  where id  in (select id from  stu where 學科='語文') and 學科 =『英語』。

分析:同時參加語文考試和英語考試的人資訊,第一步就是將考過語文的人的id查詢出來,將查詢到的結果作為一張表,然後在從這張表裡面查詢考過英語的人的資訊,最後出來的資訊,就滿足要求了。

簡單常用sql查詢

self.db executeupdate sql,record.recordid create table scene record id text primary key,record time text,location text,place text,description text,isu...

提公升MySQL查詢效能常用套路

前言 我們查詢資料庫通常會多表關聯,當資料量大時,很多時候我們可以多表查詢分成多次 sql 查詢,來提高效能。如 select from tag join tag post on tag post.tag id tag.id join post on tag post.post id post.id...

sql常用查詢

insert into t call callnumber,tellnum,startdatetime,enddatetime values 13925473495 gzsx009 2013 10 20 10 03 2013 10 22 13 06 輸出所有資料中通話時間最長的5條記錄。用order...