【问题标题】:How to use MySql Procedure to display result based on some input filters ignoring other filters if not entered on the front end by user?如果用户未在前端输入,如何使用 MySql 过程基于某些输入过滤器忽略其他过滤器来显示结果?
【发布时间】:2016-02-27 06:08:01
【问题描述】:

我想使用更详细搜索的按钮单击过程,如果我输入所有输入值,那么它会显示适当的结果。但是,我想让我的程序更加动态,这样如果我没有输入任何标题,那么它将显示基于其他过滤器的所有记录,包括游戏标题的等效值。此外,如果缺少平台或完整性或任何其他值,那么它也应该返回所有可能过滤器的查询结果。那么,如何在 MySql 中实现呢?

我想学习下面的过程调用

call sp_search_Records_on_all_conditions('S','Extreme Sports','3',' ',' ' ,'Good','2000-11-07','2011-06-15',12,67);

call sp_search_Records_on_all_conditions('S',' ','3',NULL,' ','Good','2000-11-07','2011-06-15',12,67);

以下是程序:

USE `videogame_collection_1`$$
CREATE PROCEDURE `sp_search_Records_on_all_conditions` (IN Game_Name_ip     VARCHAR(100),IN Genre_ip ENUM('Controllers', 'Extreme Sports', 'Action &     Adventure', 'Racing', 'RPG', 'Baseball', 'Sports', 'Systems', 'Puzzle',     'Fighting', 'Strategy', 'FPS', 'Wrestling', 'Accessories', 'Soccer', 'Other',     'Football', 'Party', 'Arcade', 'Basketball', 'Simulation', 'Music'),
IN Rating_ip ENUM('1', '2', '3', '4', '5'),IN Platform_Name_ip ENUM('N64',      'NES', 'Super Nintendo', 'Gamecube', 'Wii', 'Playstation 1', 'Playstation 2',     'Playstation 3', 'Xbox', 'Xbox 360', 'Sega Genesis', 'Atari 2600', 'Gameboy     Color', 'Gameboy Advance'),IN Completeness_Type_ip ENUM('B', 'I', 'C', 'BC',     'BI', 'IC', 'BIC'),     IN Condition_ip ENUM('New', 'Mint', 'Very Good', 'Good',    'Acceptable', 'Poor'),
IN from_Purchase_date_ip DATE,IN to_Purchase_date_ip DATE ,IN     from_Purchase_Price_ip DECIMAL(4,2), IN to_Purchase_Price_ip DECIMAL(4,2))
Begin
SELECT 
    video_game.Game_Name,
    video_game.Genre,
    video_game.Rating,
    platform.Platform_Name,
     mycollection.Completeness_Type,
      mycollection.`Condition`,
    mycollection.Purchase_Date,
    mycollection.Purchase_Price
        FROM
    video_game
        INNER JOIN
    video_game_platform_mycollection ON video_game.Game_Id =     video_game_platform_mycollection.Game_Id
        INNER JOIN
    platform ON video_game_platform_mycollection.Platform_Id =     platform.Platform_Id
         INNER JOIN
    mycollection ON video_game_platform_mycollection.MyCollection_Id =     mycollection.MyCollection_Id
     where video_game.Game_Name  LIKE CONCAT('%', Game_Name_ip, '%') and       video_game.Genre=Genre_ip and video_game.Rating=Rating_ip
     and platform.Platform_Name=Platform_Name_ip and      mycollection.Completeness_Type=Completeness_Type_ip and     mycollection.`Condition`= Condition_ip
    and mycollection.Purchase_Date between from_Purchase_date_ip and     to_Purchase_date_ip and   mycollection.Purchase_Price >=from_Purchase_Price_ip     and mycollection.Purchase_Price<= to_Purchase_Price_ip;
 END
$$

DELIMITER ;

【问题讨论】:

    标签: mysql stored-procedures mysql-workbench mysqldump mysql-error-1064


    【解决方案1】:

    我可能会检查 where 语句中每个参数的值。这是一个开始:

    where 
    ((Game_Name_ip is null OR len(trim(Game_Name_ip)) > 0) OR video_game.Game_Name  LIKE CONCAT('%', Game_Name_ip, '%')) and
    ...
    

    所以基本上,您将检查该值是否为 null 或仅包含空格的字符串。如果为假,则意味着有内容可供搜索,因此您将根据指定的 Game_Name_ip 参数检查实际游戏名称值。对 WHERE 语句中的每个值重复此模式。

    【讨论】:

      猜你喜欢
      • 2013-06-25
      • 2020-08-11
      • 1970-01-01
      • 1970-01-01
      • 2018-11-18
      • 1970-01-01
      • 2021-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多