mysql多表查詢語句_mysql查詢語句 和 多表關聯查詢 以及 子查詢

1.查詢一張表:select * from 表名;

2.查詢指定字段:select 字段1,字段2,字段3….from 表名;

3.where條件查詢:select字段1,字段2,字段3 frome

表名 where 條件表達式;

例:select * from t_studect where id=1;

select * from t_student where age>22;

4.帶in關鍵字查詢:select字段1,字段2

frome 表名 where 字段 [not]in(元素1,元素2);

例:select * from t_student where age in (21,23);

select

* from t_student where age not in (21,23);

5.帶between and的范圍查詢:select字段1,字段2

frome 表名 where 字段 [not]between 取值1 and 取值2;

例:select * frome t_student where age between 21 and 29;

select * frome t_student where age not between 21 and 29;

6.帶like的模糊查詢:select字段1,字段2…

frome 表名 where 字段 [not] like ‘字符串’;

“%”代表任意字符;

“_”代表單個字符;

例:select * frome t_student where stuName like ‘張三”;

select * frome t_student where

stuName like ‘張三%”;

select

* frome t_student where stuName like ‘%張三%”;//含有張三的任意字符

select * frome t_student where

stuName like ‘張三_”

7.空值查詢:select字段1,字段2…frome

表名 where 字段 ?is[not] null;

8.帶and的多條件查詢:

select字段1,字段2…frome

表名 where 條件表達式1 and 條件表達式2 [and 條件表達式n]

例:select * frome t_student where gradeName=’一年級’ and age=23;

9.帶or的多條件查詢

select字段1,字段2…frome

表名 where 條件表達式1 or 條件表達式2 [or 條件表達式n]

例:select * frome t_student where gradeName=’一年級’ or age=23;//或者,條件只要滿足一個

10.distinct去重復查詢:select distinct 字段名 from 表名;

11.對查詢結果排序order by:select 字段1,字段2…from 表名 order by 屬性名 [asc|desc]

例:select * frome t_student order by age desc;//降序,從大到小

select * frome t_student order

by age asc;//升序,asc默認可以不寫

12.分組查詢group by

group by 屬性名 [having 條件表達式][with rollup]

1.單獨使用(毫無意義,不能單獨使用);

2.與group_concat()函數一起使用;

例:select gradeName,group_concat(stuName) from t_student group by gradeName;

28db674771d590f1336e3b0f9051e212.png

3.與聚合函數一起使用;

例:select gradeName,count(stuName) from t_student group by gradeName;

55b64299d50a04381747e5dc84272146.png

4.與having一起使用(顯示輸出的結果);

例:select gradeName,count(stuName) from t_student group by gradeName having count(stuName)>3;

7d7dc2c315eec7cbdc37c432ad3af221.png

5.與with rollup 一起使用(最后加入一個總和行);

例:select gradeName,group_concat(stuName) from t_student group by gradeName with rollup;

9a759e6d87da503234a59f6a528a4a61.png

13.limit 分頁查詢:select 字段1,字段2,…from 表名 limit 初始位置,記錄數;

例子:select * from t_student limit 0,5;

多表連接查詢

表一:t_book

d1dd0c19b45fcd24034a0b3e1b5e22ef.png

表二:t_bookType

ff1f6eef999fab34f6c49354680aa2ab.png

表三:t_priceLevel

56d0376e22bc0dc8dcfcb6744f79aad9.png

select * from t_book,t_bookType;

e0b53b4b783ce06d7cc17a91f3f563eb.png

1.內連接查詢(兩張或以上的表連接起來查詢需要的數據)

根據表一的bookTypeId查詢出所有bookTypeName

select * from t_book,t_bookType where t_book.bookTypeId=t_bookType.id;

156a329266ad49ad4c36d0588cfc568f.png

查詢某幾個字段:

select bookNme,author from t_book,t_bookType where t_book.bookTypeId=t_bookType.id;

63dc41a007616dffc5067c424ef00ecc.png

2.外連接查詢(兩張或以上的表連接起來查詢某張表的信息)

3.左連接查詢

select * from t_book?left?join?t_bookType?on?t_book.bookTypeId=t_bookType.id;

如下圖:表一(左邊表)t_book的數據全部查出 表二沒有的字段用null代替

fb9385bc292fd7b0dd8c4f5efcde2cb3.png

4.右連接查詢

select * from t_book?right?join?t_bookType?on?t_book.bookTypeId=t_bookType.id;

查出表二(右邊表)的所有信息,表一沒有的用null代替

35115bc934a45ff7f7ddb75346e554e4.png

5.多條件連接查詢

select * from t_book,t_bookType where t_book.bookTypeId=t_bookType.id?and?t_book.price>70;

b99334c0518c3815509783d94ed90757.png

子查詢

1.帶in關鍵字的子查詢(一個查詢語句的條件可能落在另一個select語句的查詢結果中)

select * from t_book where bookType?in(select id from t_bookType);

select * from t_book where bookType?not?in(select id from t_bookType);

2.帶比較運算符的子查詢(子查詢可以使用比較運算符)

select * from t_book where price>=(select price from t_priceLevel where priceLevel=1);

3.帶exists關鍵字的子查詢(加入子查詢查詢到記錄,則進行外層查詢,否則,不執行外層查詢)

select * from t_book where?exists(select * from t_booktype);

select * from t_book where?not exists(select * from t_booktype);

4.帶any關鍵字的子查詢(any關鍵字表示滿足其中任一條件)

select * from t_book where price>=any(select price from t_priceLevel);

5.帶all關鍵字的子查詢(all關鍵字表示滿足所有條件)

select * from t_book where price>=all(select price from t_priceLevel);

合并查詢

1.union

使用union關鍵字是,數據庫系統會將所有的查詢結果合并到一起,然后去掉相同的記錄;

select id from t_bookunionselect id from t_bookType;

2.union all

使用union all,不會去除掉重復的記錄;

select id from t_bookunion all?select id from t_bookType;

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/260485.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/260485.shtml
英文地址,請注明出處:http://en.pswp.cn/news/260485.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Pytorch 自定義激活函數前向與反向傳播 sigmoid

文章目錄Sigmoid公式求導過程優點:缺點:自定義Sigmoid與Torch定義的比較可視化import matplotlib import matplotlib.pyplot as plt import numpy as np import torch import torch.nn as nn import torch.nn.functional as F%matplotlib inlineplt.rcPa…

SVN錯誤:Attempted to lock an already-locked dir

出現這個問題后使用“清理”功能,如果還不行,就直接到上一級目錄,再執行“清理”,然后再“更新”。有時候如果看到某個包里面的文件夾沒有SVN的標志,直接用“CtrlDelete”手工刪除,然后“清理”&#xff0c…

js高級編程_這位設計師用Processing把創意編程玩到了極致!

Processing作為新媒體從業者的必備工具,近來卻越來越成為設計師們的新寵!今天小編將介紹以為用Processing把創意編程玩到極致的設計師Tim Rodenbrker。“我們的世界正在以驚人的速度變化。新技術為創作帶來了根本性的轉變。編程是我們這個時代最寶貴的技…

微軟.NET Framework 4.5.2 RTM正式版

今天,微軟.NET開發團隊發布.NET Framework 4.5.2 RTM正式版。新版框架繼續高度兼容現有的.NET Framework 4、4.5、4.5.1等版本,該版本框架與舊版的.NET Framework 3.5 SP1和早期版本采取不同的處理方式,但與.NET Framework 4、4.5相比&#x…

HDU 1042 N!(高精度計算階乘)

N! Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 34687 Accepted Submission(s): 9711 Problem DescriptionGiven an integer N(0 ≤ N ≤ 10000), your task is to calculate N!InputOne N in one line, pr…

使用WebDriver遇到的那些坑

http://blog.csdn.net/oWuFeng1/article/category/2722111 在做web項目的自動化端到端測試時主要使用的是Selenium WebDriver來驅動瀏覽器。Selenium WebDriver的優點是支持的語言多,支持的瀏覽器多。主流的瀏覽器Chrome、Firefox、IE等都支持,手機上的瀏…

python的閉包要素_Python的閉包

我的理解,Python中的閉包和其他語言中的閉包一樣,都是在一個函數中返回另一個函數。def out_fun(num): print(------1-----) def in_fun(in_num): print(---------2--------) print(in_num%d % in_num) return num in_num print(-------3--------) retu…

Pytorch 自定義激活函數前向與反向傳播 Tanh

看完這篇,你基本上可以自定義前向與反向傳播,可以自己定義自己的算子 文章目錄Tanh公式求導過程優點:缺點:自定義Tanh與Torch定義的比較可視化import matplotlib import matplotlib.pyplot as plt import numpy as np import torc…

multi mysql_mysqld_multi 的使用方法

mysqld_multi 的使用方法:官方文檔:https://dev.mysql.com/doc/refman/5.7/en/mysqld-multi.html 【文檔有些問題,按照它的這個配置,mysqld_multi無法關閉實例】mysqld_multi無法關閉實例的解決方法:https://bugs.mysql.com/bug…

vsftp 無法啟動,500 OOPS: bad bool value in config file for: anonymous_enable

朋友的FTP啟動不了,叫我幫他看,啟動時出現以下錯誤信息: 500 OOPS: bad bool value in config file for: anonymous_enable 看似配置文件錯誤,看了一下配置相應的行: anonymous_enableNO 語句沒什么錯誤,不…

HDU ACM 1181 變形課 (廣搜BFS + 動態數組vector)-------第一次使用動態數組vector

http://acm.hdu.edu.cn/showproblem.php?pid1181 題意&#xff1a;給我若干個單詞,若單詞A的結尾與單詞B的開頭相同,則表示A能變成B,判斷能不能從b開頭變成m結尾. 如: big-got-them 第一次使用動態數組vector View Code 1 #include <iostream>2 #include <vector>…

Max Sum 杭電 1003

2019獨角獸企業重金招聘Python工程師標準>>> #題目概述 題目的意思是給你一個數列&#xff0c;找到一個子數列&#xff0c;這個子數列的和是所有子數列中和最大的。 當然把數列的所有數都列出來肯定不現實。 黑黑&#xff0c;不知道正不正確&#xff0c;我是先從第一…

shiro反序列化工具_Apache Shiro 1.2.4反序列化漏洞(CVE-2016-4437)源碼解析

Apache ShiroApache Shiro是一個功能強大且靈活的開源安全框架,主要功能包括用戶認證、授權、會話管理以及加密。在了解該漏洞之前,建議學習下Apache Shiro是怎么使用.debug環境jdk1.8Apache Shiro 1.2.4測試demo本地debug需要以下maven依賴<!-- https://mvnrepository.com/…

window 下的mysql_Windows下MySQL下載安裝、配置與使用

用過MySQL之后&#xff0c;不論容量的話&#xff0c;發現比其他兩個(sql server 、oracle)好用的多&#xff0c;一下子就喜歡上了。下面給那些還不知道怎么弄的童鞋們寫下具體的方法步驟。(我這個寫得有點太詳細了&#xff0c;甚至有些繁瑣&#xff0c;有很多步驟在其他的教程文…

H264視頻通過RTMP直播

http://blog.csdn.net/firehood_/article/details/8783589 前面的文章中提到了通過RTSP&#xff08;Real Time Streaming Protocol&#xff09;的方式來實現視頻的直播&#xff0c;但RTSP方式的一個弊端是如果需要支持客戶端通過網頁來訪問&#xff0c;就需要在在頁面中嵌入一個…

Pytorch 自定義激活函數前向與反向傳播 ReLu系列 含優點與缺點

文章目錄ReLu公式求導過程優點&#xff1a;缺點&#xff1a;自定義ReLu與Torch定義的比較可視化Leaky ReLu PReLu公式求導過程優點&#xff1a;缺點&#xff1a;自定義LeakyReLu與Torch定義的比較可視化自定義PReLuELU公式求導過程優點缺點自定義LeakyReLu與Torch定義的比較可視…

手勢處理

在ios開發中&#xff0c;需用到對于手指的不同操作&#xff0c;以手指點擊為例&#xff1a;分為單指單擊、單指多擊、多指單擊、多指多擊。對于這些事件進行不同的操作處理&#xff0c;由于使用系統自帶的方法通過判斷touches不太容易處理&#xff0c;而且會有事件之間的沖突。…

mybatis select count(*) 一直返回0 mysql_Mybatis教程1:MyBatis快速入門

點擊上方“Java技術前線”&#xff0c;選擇“置頂或者星標”與你一起成長一、Mybatis介紹MyBatis是一個支持普通*SQL*查詢&#xff0c;存儲過程和高級映射的優秀持久層框架。MyBatis消除了幾乎所有的JDBC代碼和參數的手工設置以及對結果集的檢索封裝。MyBatis可以使用簡單的XML…

css預處理器sass使用教程(多圖預警)

css預處理器賦予了css動態語言的特性&#xff0c;如變量、函數、運算、繼承、嵌套等&#xff0c;有助于更好地組織管理樣式文件&#xff0c;以及更高效地開發項目。css預處理器可以更方便的維護和管理css代碼&#xff0c;讓整個網頁變得更加靈活可變。對于預處理器&#xff0c;…

mysql 主從優點_MySql主從配置實踐及其優勢淺談

1、增加兩個MySQL,我將C:\xampp\mysql下的MYSQL復制了一份&#xff0c;放到D:\Mysql2\Mysql5.1修改my.ini(linux下應該是my.cnf)&#xff1a;[client]port 3307[mysqld]port 3307basedirD:/Mysql2/Mysql5.1/mysqldatadirD:/Mysql2/Mysql5.1/mysql/data/之后&#xff0c;再增加…