0

我需要将其转换为我的 SQL 中的存储过程

SELECT Movie_ID
FROM Movie
WHERE MovieName = 'The Matrix'
 AND Shop_ID IN (
  SELECT Shop_ID
  FROM Shop
  WHERE Location = 'NY')

我只是写吗

CREATE PROCEDURE SearchQuery(
SELECT Movie_ID
FROM Movie
WHERE MovieName = 'The Matrix'
 AND Shop_ID IN (
  SELECT Shop_ID
  FROM Shop
  WHERE Location = 'NY')

或者怎么做?最后我想用movieNameand调用这个过程StoreLocation,但是我该怎么做呢?

该程序应该告诉我电影是否在该地点出租:)

4

2 回答 2

1

这是你要找的吗?

DELIMITER //
CREATE PROCEDURE yourProc( In Movie varchar(100), In Location varchar(100) )
BEGIN
  SELECT Movie_ID
  FROM Movie
  WHERE MovieName = Movie
   AND Shop_ID IN (
    SELECT Shop_ID
    FROM Shop
    WHERE Location = Location)
END//
DELIMITER ;

以下是有关程序的一些文档:

http://dev.mysql.com/doc/connector-net/en/connector-net-tutorials-stored-procedures.html


编辑:要从您的过程中返回可用或不可用的值,您可以将查询更改为使用 aLEFT JOIN而不是,IN并添加一条CASE语句以返回适当的值。

像这样的东西应该工作:

SELECT CASE WHEN S.Location IS NULL THEN 'Unavailable' ELSE 'Available' END 
FROM Movie M
    LEFT JOIN Shop S ON 
        M.Shop_Id = S.Shop_Id AND
        S.Location = Loc
WHERE M.MoveName = Movie

您可能需要也可能不需要添加DISTINCT到您的查询中。

于 2013-05-26T20:18:45.137 回答
1

Movie_ID如果有与您的选择匹配的记录,则以下查询将返回,否则返回 -1:

CREATE FUNCTION search_query(p_movie_name VARCHAR(255), p_store_location VARCHAR(255)) 
RETURNS BOOL // edit
BEGIN
    DECLARE result INT DEFAULT -1;
    SELECT Movie_ID INTO result
        FROM Movie
        WHERE MovieName = p_movie_name AND Shop_ID IN (
            SELECT Shop_ID
            FROM Shop
            WHERE Location = p_location);
    RETURN result <> -1; // edit
END

请注意,您必须将查询分隔符更改为其他内容,而不是;通过 PhpMyAdmin 输入存储过程或使用

DELIMITER //
CREATE FUNCTION...

END//
DELIMITER;
于 2013-05-26T20:18:54.827 回答