我需要将它转换为我的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')
或者怎么样?最后,我想用movieName
和StoreLocation
来调用该过程,但我该如何做?
该程序应告诉我该电影是否在该地点出租:)
答案 0 :(得分: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
编辑:要返回您的程序中可用或不可用的值,您可以将查询更改为使用LEFT 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
。
答案 1 :(得分: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;