从 table 中获取 value/values
Fetch value/values from table
我的数据库中有以下 table
CREATE TABLE `sms_pool` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`ag_id` VARCHAR(20) NOT NULL,
`sms_to` VARCHAR(15) NOT NULL,
`template_name` VARCHAR(100) NOT NULL,
`contents` VARCHAR(500) NOT NULL,
`bulk_flag` VARCHAR(1) NOT NULL,
`file_name` VARCHAR(100) NULL DEFAULT NULL,
`send_flag` VARCHAR(1) NOT NULL,
`creation_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`created_by` VARCHAR(20) NOT NULL,
`processing_msg` VARCHAR(2000) NULL DEFAULT NULL,
PRIMARY KEY (`id`),
);
我想写一个 procedure/function 以 'id' 作为输入。
如果 'id' 等于 table 中的任何 id 那么它应该 return 相应的行,
if 'id' = NULL 那么它应该 return 数据库中的所有行。
注意 :如果 'id' 不存在于 table 中,那么它应该 return 所有行。
我应该怎么做?任何帮助表示赞赏。先感谢您。 :D
create procedure sp_get_data(in in_id int)
begin
declare temp_id int;
set temp_id =(select count(*) from table_name where id =in_id);
if(temp_id) >0
Then
select * from table_name where id = in_id ;
else
select * from table_name ;
end
end
我的数据库中有以下 table
CREATE TABLE `sms_pool` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`ag_id` VARCHAR(20) NOT NULL,
`sms_to` VARCHAR(15) NOT NULL,
`template_name` VARCHAR(100) NOT NULL,
`contents` VARCHAR(500) NOT NULL,
`bulk_flag` VARCHAR(1) NOT NULL,
`file_name` VARCHAR(100) NULL DEFAULT NULL,
`send_flag` VARCHAR(1) NOT NULL,
`creation_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`created_by` VARCHAR(20) NOT NULL,
`processing_msg` VARCHAR(2000) NULL DEFAULT NULL,
PRIMARY KEY (`id`),
);
我想写一个 procedure/function 以 'id' 作为输入。
如果 'id' 等于 table 中的任何 id 那么它应该 return 相应的行,
if 'id' = NULL 那么它应该 return 数据库中的所有行。
注意 :如果 'id' 不存在于 table 中,那么它应该 return 所有行。
我应该怎么做?任何帮助表示赞赏。先感谢您。 :D
create procedure sp_get_data(in in_id int)
begin
declare temp_id int;
set temp_id =(select count(*) from table_name where id =in_id);
if(temp_id) >0
Then
select * from table_name where id = in_id ;
else
select * from table_name ;
end
end