Mysql游標查詢結果合併

2021-10-02 23:50:08 字數 1638 閱讀 1911

drop

procedure

ifexists insert_extra_column;

delimiter

//create

procedure insert_extra_column(

)begin

##定義判斷變數

declare _flag varchar(50

);## 定義查詢變數

declare _cur cursor

forselect

`code`

from location l where l.deleted =

0and l.location_type=

'm';

### 迴圈賦初始值

declare

continue

handler

fornot found set _flag=

null

;##建立臨時表儲存每次游標查詢結果

create

temporary

table result_temp(

location_code varchar(50

),diagnosis_count int

, remote_count int

, service_type varchar(20

))engine

=innodb

default

charset utf8;

### 開啟

open _cur;

## 賦值

fetch _cur into _flag;

###迴圈判斷

while

(_flag is

notnull)do

insert

into result_temp

select

_flag as location_code,

(select

count(*

)from visit v where v.deleted =

0and v.report_doctor is

notnull

and v.location_code = _flag and v.high_location_code is

null

)as diagnosis_count,

(select

count(*

)from visit v where v.deleted =

0and v.report_doctor is

notnull

and v.location_code = _flag and v.high_location_code is

notnull

)as remote_count,

'rad'

as service_type;

## 賦值下乙個游標

fetch _cur into _flag;

endwhile

;## 關閉

close _cur;

select

*from result_temp;

end//

delimiter

;

Mysql合併查詢結果

本文通過例項介紹mysql中的group concat函式的使用方法,比如select group concat name mysql中group concat函式 完整的語法如下 group concat distinct 要連線的字段 order by asc desc 排序字段 separat...

mysql 合併兩個查詢結果

顯然,查詢結果的time為主鍵,目的是將兩個查詢結果合成乙個結果。如果在 中實現,一次便利乙個表新增到另外乙個表中,效率非常低。那麼在mysql 中實現方式為 出金 withdraw select from select date complete time as time,sum amt as a...

mysql游標結果為空

定義游標時直接查詢資料時,結果為空,解決方法如下 定義游標名字為c result declare c result cursor for select table name,column name from information schema.columns where table schema ...