mysql 儲存過程模擬給某個使用者傳送郵件

2021-10-16 12:54:34 字數 1713 閱讀 5794

drop table if exists user_info

drop table if exists email_info

#使用者表

create

table user_info(

id int

notnull

auto_increment

primary

key,

name varchar(30

),email varchar(50

))default

charset

=utf8;

insert

into user_info(id,name,email)

values(1

,'lau'

,'[email protected]');

insert

into user_info(id,name,email)

values(2

,'張三'

,'[email protected]');

#郵件表

create

table email_info(

id int

notnull

auto_increment

primary

key,

email varchar(50

),content text

, send_time datetime

)default

charset

=utf8;

那麼,就可以在user_info表中,通過id獲得郵箱,比如

select email from user_info where id=

1;

郵件的內容為

insert

into email_info(email,content,send_time)

values

('[email protected]'

,'歡迎加入'

,now()

);

那麼如何使用儲存過程實現上面的功能呢

儲存過程實現:根據使用者id和郵件內容content給使用者發郵件

delimiter

//create

procedure send_email(

in user_id int

,in content text

character

set utf8)

#明確引數編碼,有時傳參會出現亂碼

begin

/* 根據使用者id查詢郵箱email */

set@user_email=(

select email from user_info where id=user_id)

;#@user_mail是儲存過程的內部變數

/* 模擬傳送郵件 */

insert

into email_info(email,content,send_time)

values

(@user_email

,content,

now())

;end;//

delimiter

;call send_email(1,

'歡迎加入'

);

mysql建立儲存過程,模擬插入200萬使用者

執行完語句後會建立相關的儲存過程,需要執行語句 call test 才可以執行插入操作。注意 建立儲存過程語句和call執行語句必須分開執行,建立儲存過程語句成功後,再執行呼叫儲存過程語句 call test create procedure test 代表建立儲存過程。test未儲存過程的名字可以...

用 PHP 呼叫 MySQL 儲存過程

mysql 5.0 以上支援儲存過程。php 5.0 以上的 mysqli 系列函式可以支援操作 mysql 的儲存過程。以下是一些簡單的儲存過程和用 php 呼叫的示例。一 返回單個資料 1 2 header content type text html charset utf 8 3 4 hos...

用 PHP 呼叫 MySQL 儲存過程

mysql 5.0 以上支援儲存過程。php 5.0 以上的 mysqli 系列函式可以支援操作 mysql 的儲存過程。以下是一些簡單的儲存過程和用 php 呼叫的示例。一 返回單個資料 1 host localhost 5 user root 6 password mypassword 7 db...