gpt4 book ai didi

mysql - 如果条件为真,则在 MySQL 存储过程中运行查询

转载 作者:搜寻专家 更新时间:2023-10-30 20:05:44 28 4
gpt4 key购买 nike

我正在使用 MySQL 数据库并尝试创建一个存储过程。我怎样才能做到,如果 query1 的结果没有记录,那么它会执行不同的查询?

这是我目前所拥有的:

/* CREATE DB */
CREATE DATABASE mydata;
use mydata;
/* TABLE */
CREATE TABLE mydata (
ID BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(255) NOT NULL,
Value VARCHAR(255) NOT NULL
) ENGINE=InnoDB;
INSERT INTO mydata (Name, Value) VALUES ("testname", "testvalue");

/* STORED PROCEDURE */
delimiter //
CREATE PROCEDURE myproc(IN myTable VARCHAR(255),
IN myValue VARCHAR(255),
IN myValueTwo VARCHAR(255))
BEGIN
SET @iTable=myTable;
SET @iValue=myValue;
SET @iValueTwo=myValueTwo;

SET @query = CONCAT('SELECT Name FROM ', @iTable,
' WHERE Value="', @iValue, '"');
SET @querytwo = CONCAT('SELECT Name FROM ', @iTable,
' WHERE Value="', @iValueTwo, '"');
PREPARE QUERY FROM @query;
EXECUTE QUERY;

END //
delimiter ;

/* CALL */
call myproc("mydata", "testvalue", "");

我想运行一个查询,并且仅当第一个查询没有行时才执行第二个查询。执行此操作的最佳方法是什么?

最佳答案

这需要一些工作,但我做了足够的调整。您的代码问题与您的逻辑无关,而与 MySQL 存储过程语言本身有关。在执行动态 SQL 时会出现范围问题。

我所做的是创建一个临时表并将返回值存入其中

这里是加载的一些示例数据

mysql> drop database if exists user391986;
Query OK, 1 row affected (0.08 sec)

mysql> create database user391986;
Query OK, 1 row affected (0.00 sec)

mysql> use user391986
Database changed
mysql> CREATE TABLE mytable (
-> ID BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> Name VARCHAR(255) NOT NULL,
-> Value VARCHAR(255) NOT NULL
-> ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.11 sec)

mysql> INSERT INTO mytable (Name,Value) VALUES
-> ('rolando','edge'),('pamela','washington'),
-> ('dominique','wilkins'),('diamond','cutter');
Query OK, 4 rows affected (0.06 sec)
Records: 4 Duplicates: 0 Warnings: 0

mysql> SELECT * from mytable;
+----+-----------+------------+
| ID | Name | Value |
+----+-----------+------------+
| 1 | rolando | edge |
| 2 | pamela | washington |
| 3 | dominique | wilkins |
| 4 | diamond | cutter |
+----+-----------+------------+
4 rows in set (0.00 sec)

mysql>

这里是经过调整以捕获临时表中的返回值的存储过程

mysql> delimiter //
mysql> CREATE PROCEDURE myproc(IN myTable VARCHAR(255), IN myValue VARCHAR(255), IN myValueTwo VARCHAR(255))
-> BEGIN
-> DECLARE foundcount INT;
-> DECLARE retval VARCHAR(255);
->
-> SET @iTable=myTable;
-> SET @iValue=myValue;
-> SET @iValueTwo=myValueTwo;
->
-> CREATE TEMPORARY TABLE IF NOT EXISTS mynumber (rv VARCHAR(255)) ENGINE=MEMORY;
-> DELETE FROM mynumber;
->
-> SET retval = 'nothing retrieved';
-> SET @query = CONCAT('INSERT INTO mynumber SELECT Name FROM ', @iTable, ' WHERE Value=''', @iValue, '''');
-> PREPARE QUERY FROM @query;
-> EXECUTE QUERY;
-> DEALLOCATE PREPARE QUERY;
-> SELECT COUNT(1) INTO foundcount FROM mynumber;
-> IF foundcount = 0 THEN
-> SET @querytwo = CONCAT('INSERT INTO mynumber SELECT Name FROM ', @iTable, ' WHERE Value=''', @iValueTwo, '''');
-> PREPARE QUERY FROM @querytwo;
-> EXECUTE QUERY;
-> DEALLOCATE PREPARE QUERY;
-> END IF;
-> SELECT COUNT(1) INTO foundcount FROM mynumber;
-> IF foundcount > 0 THEN
-> SELECT rv INTO retval FROM mynumber;
-> END IF;
-> SELECT retval;
->
-> END //
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ;
mysql>

好的,我调用了存储过程三次。第一个什么也得不到。第二个获得第二个值。第三个获取第一个值。

mysql> CALL myproc('mytable','pamela','diamond');
+-------------------+
| retval |
+-------------------+
| nothing retrieved |
+-------------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.02 sec)

mysql> CALL myproc('mytable','pamela','wilkins');
+-----------+
| retval |
+-----------+
| dominique |
+-----------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

mysql> CALL myproc('mytable','edge','wilkins');
+---------+
| retval |
+---------+
| rolando |
+---------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.02 sec)

mysql>

试一试!!!

关于mysql - 如果条件为真,则在 MySQL 存储过程中运行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9845171/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com