1. 程式人生 > 其它 >微控制器程式底層BUG問題,及解決方案

微控制器程式底層BUG問題,及解決方案

  多表查詢

  一.連線查詢

  1.內連線

    用左邊表的記錄去匹配右邊表的記錄,符合條件的才顯示,內連線又分隱式內連線和顯示內連線兩種

  1)隱式內連線  不使用join關鍵字,條件使用where指定

  語法:select 欄位名 from 左表,右表 where 條件

  ps:查詢員工的姓名和所在部門的名稱

  select a.id,dep_id,name from employee a,department b where a.dep_id=b.id

  

  2)顯示內連線  使用innet join ... on語句

  語法:select 欄位名 from 左表 join 右表 on 條件

  ps:查詢 小小度的id,年齡,姓名,工資,入職日期,部門

select a.id,a.age,a.name,a.salary,a.add_company,b.dep_name from
employee a join department b on a.dep_id=b.id and a.name='小小度'

  

  

  2.外連線

  1)左外連線(顯示左表所有資料,包括null)  使用 left join ... on

  語法:select 欄位名 from 左表 left join 右表 on 條件

  2)右外連線(顯示右表所有資料,包括null)  使用 right join ... on

  語法:select 欄位名 from 左表 right join 右表 on 條件

  ps:當有一個員工沒有部門的時候使用以下語句查詢不到這個員工(右邊為查詢的表)

select a.*,b.dep_name from employee a,department b where a.dep_id=b.id

  

  所以我們需要使用左外連結或者右外連結查詢所有員工(如圖)

select a.*,b.dep_name from employee a left join department b on a.dep_id=b.id  //左外連結
select a.*,b.dep_name from
department b right join employee a on a.dep_id=b.id 
//右外連結

  

  二.子查詢

  一條select語句結果作為另一條select語句的一部分

  語法:select 欄位 from 表 where 欄位 運算子(select 欄位 from 表)

  ps:查詢工資最高的員工資訊

select a.*,b.dep_name from employee a,department b where salary=(select max(salary) from employee) and a.dep_id=b.id

  

ps:查詢工資高於小小度(工資6543)的員工
   select *from employee a where salary>(select salary from employee where name='小小度') //大於
  
select *from employee a where salary<(select salary from employee where name='小小度') //小於

   

ps:查詢工資大於6000的人來自那些部門
    select a.*,b.dep_name from employee a,department b where salary>6000 and a.dep_id=b.id

   

ps:統計各部門的人數
    select d.dep_name 部門, (SELECT count(*) from employee e WHERE e.dep_id=d.id) 人數 from department d