mysql基礎查詢和條件查詢

2022-07-01 08:57:08 字數 3003 閱讀 6224

資料庫的好處

1.持久化資料到本地

2.可以實現結構化查詢,方便管理

資料庫的相關概念

sql優點:

資料庫儲存資料的特點

mysql服務的啟動和停止

mysql服務端的登入和退出

退出:exit

檢視mysql資料庫的版本:

mysql的常用命令

create table 表名(

​ 列名 列型別,

​ 列名 列型別,

​ …);

檢視表結構:desc 表名;

mysql語法規範

1.不區分大小寫,建議關鍵字大寫,表名、列名小寫

2.每句話用;或\g結尾

3.每條命令根據需要,各子句一般分行寫,關鍵字不能縮寫也不能分行

4.注釋

1.單行注釋:#注釋文字

2.單行注釋:-- 注釋文字(要有空格)

3.多行注釋:/* 注釋文字 */

dql(data query language)資料查詢語言

1. 基礎查詢

select 查詢列表

from 表名;

注意:在進行查詢操作之前要指定所有的庫:use myemployees;

查詢表中的單個字段:select last_name from employees;

查詢表中的多個字段:select last_name, salary, email from employees;

查詢表中的所有字段:select * from employees;

按f12進行格式化

著重號`用來區分是否是關鍵字或者字段

選中語句進行執行或f9

查詢常量值:

select 100;

select 『john』;

方式1:

select 100%98 as 結果;

select last_name as 姓, first_name as 名 from employees;

方式2:

select last_name 姓, first_name 名 from employees;

如果別名有特殊符號要加雙引號:

select salary as 「out put」 from employees;

查詢員工表中涉及到的所有部門編號:select distinct department_id from employees;

查詢員工的名和姓連線成乙個字段,並顯示為姓名:select concat(last_name,first_name) as 姓名 from employees;

select ifnull(commission_pct, 0) as 獎金率, commission_pct from employees;

2. 條件查詢

按邏輯表示式篩選:

模糊查詢

like

between and

inis null

按邏輯表示式篩選:

模糊查詢

查詢員工名中包含字元a的員工資訊:

select * from employees where last_name like '%a%';

select last_name, salary from employees where last_name like '__n_l%';

select last_name from employees where last_name like '_\_ %';

select last_name from employees where last_name like '_$_%' escape '$';

select * from employees where employee_id >= 100 and employee_id <= 120;

select * from employees where employee_id between 100 and 120;

select last_name, job_id from employees where job_id = 'it_prog' or job_id = 'ad_vp' or job_id = 'ad_pres';

select last_name, job_id from employees where job_id in ('it_prog', 'ad_vp', 'ad_pres');

select

last_name,

commission_pct

from

employees

where

commission_pct is null;

select

last_name,

commission_pct

from

employees

where

commission_pct is not null;

測試題

select

salary,

last_name

from

employees

where commission_pct is null

and salary < 18000;

select

* from

employees

where job_id <> 'it'

or salary = 12000 ;

desc departments;

select distinct

location_id

from

departments ;

MySQL條件查詢和範圍查詢

一 比較運算子 運算子功能 示例 大於select from student where age 14 小於select from student where age 18 大於等於 select from student where age 15 小於等於 select from student ...

MySQL基礎筆記 條件查詢

語法 使用where關鍵字 select 查詢列表 from 表名 where 篩選條件 按條件表示式篩選 條件運算子 sql語句的不等號一般寫為 而不用!按邏輯表示式篩選 邏輯運算子 and or not 3 模糊查詢 like between and in is null 一 按表示式篩選 篩選...

mysql基礎複習 條件查詢

selece 查詢列表 from 表名 where 篩選條件 篩選條件分類1 條件運算子 大於 小於 大於等於 小於等於 2 邏輯表示式 與 或 and 或 或 or 非 或 not 3 模糊查詢 like 指定子句的查詢模式,一般配合萬用字元使用 between num1 and num2 操作符...