gpt4 book ai didi

带参数语法的 MySQL 过程

转载 作者:行者123 更新时间:2023-11-29 19:14:50 29 4
gpt4 key购买 nike

我有一个架构,其中包含星球大战中的人物列表、他们出现的电影、他们访问的行星等。这是架构:

CREATE DATABASE  IF NOT EXISTS `starwarsFINAL` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `starwarsFINAL`;

DROP TABLE IF EXISTS `characters`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `characters` (
`character_name` varchar(45) NOT NULL,
`race` varchar(45) DEFAULT NULL,
`homeworld` varchar(45) DEFAULT 'Unknown',
`affiliation` varchar(45) DEFAULT NULL,
PRIMARY KEY (`character_name`),
KEY `planet_fk` (`homeworld`),
CONSTRAINT `planet_fk` FOREIGN KEY (`homeworld`) REFERENCES `planets` (`planet_name`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

DROP TABLE IF EXISTS `movies`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `movies` (
`movie_id` int(11) NOT NULL,
`title` varchar(128) DEFAULT NULL,
`scenes_in_db` int(11) DEFAULT NULL,
`scenes_in_movies` int(11) DEFAULT NULL,
PRIMARY KEY (`movie_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

DROP TABLE IF EXISTS `planets`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `planets` (
`planet_name` varchar(45) NOT NULL,
`planet_type` varchar(30) DEFAULT NULL,
`affiliation` varchar(30) DEFAULT NULL,
PRIMARY KEY (`planet_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

DROP TABLE IF EXISTS `timetable`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `timetable` (
`character_name` varchar(45) DEFAULT NULL,
`planet_name` varchar(45) DEFAULT 'Unknown',
`movie_id` int(11) DEFAULT NULL,
`arrival` int(11) DEFAULT NULL,
`departure` int(11) DEFAULT NULL,
`time_id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`time_id`),
UNIQUE KEY `timetable_un` (`character_name`,`planet_name`,`movie_id`,`arrival`),
KEY `timetable_fkplanet` (`planet_name`),
KEY `timetable_fkmovie` (`movie_id`),
CONSTRAINT `timetable_fkcharacter` FOREIGN KEY (`character_name`) REFERENCES `characters` (`character_name`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `timetable_fkmovie` FOREIGN KEY (`movie_id`) REFERENCES `movies` (`movie_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `timetable_fkplanet` FOREIGN KEY (`planet_name`) REFERENCES `planets` (`planet_name`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=57 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

这是我的问题:

Write a procedure track_planet(planet) that accepts a planet name and returns a result set that contain the planet name, the movie name, and the number of characters that appear on that planet during that movie.

这是我到目前为止所拥有的,但我迷失/卡住了如何让它发挥作用。 (我是 MySQL 新手)

DROP PROCEDURE IF EXISTS track_planet;
DELIMITER $$
CREATE PROCEDURE track_planet(IN planet VARCHAR(45))
BEGIN
SELECT planet_name FROM planets, title FROM movies,
COUNT(DISTINCT character_name) FROM characters WHERE planet_name = planet;
END
DELIMITER;

最佳答案

你好_ friend 。

首先感谢您发布数据库架构,它确实很有帮助。由此我可以制作这张图片:

enter image description here

我已将您的代码更改为:

DELIMITER $$

DROP PROCEDURE IF EXISTS track_planet $$

CREATE PROCEDURE track_planet(IN planet VARCHAR(45))
BEGIN
SELECT
tt.planet_name as planetName, m.title as movieName, COUNT(tt.character_name) as characters
FROM timetable tt
LEFT JOIN movies m ON tt.movie_id = m.movie_id
WHERE tt.planet_name = planet
GROUP BY planetName, movieName;
END $$

DELIMITER ;

除了少量修改之外,函数声明的代码几乎相同。

主要变化是 SELECT 语句:

  1. 选择必要的列;
  2. 看来您在timetable中掌握了大部分信息,因此只需加入movies即可获取电影名称;
  3. 首先在星球名称上使用 GROUP BY,然后在电影名称上使用,以便按特定电影计算特定星球上的字符

就是这样......

希望这对您有帮助。让我知道它是否适合您。

干杯!

关于带参数语法的 MySQL 过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42796238/

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