gpt4 book ai didi

mysql - 如何在未输入任何内容时获取 MySQL 查询并返回所有结果

转载 作者:行者123 更新时间:2023-11-29 22:35:10 24 4
gpt4 key购买 nike

所以我正在尝试制作一个相当基本的搜索功能。

到目前为止我得到的是 4 个输入。搜索字符串和三个数字存储的下拉列表。用户输入文本,然后选择任何下拉列表或不选择任何下拉列表来缩小搜索范围。

问题是,每当我尝试调整查询以包含更多文本字段时,我都无法让它按我想要的方式工作。

我想要做的是将所有语句视为当前的 LIKE 语句,如果没有找到文本,则返回所有结果。我现在对每个子查询使用两个 OR 语句得到的是,当用户没有从三个下拉列表中选择值时,查询将失败并发生错误。我该如何解决这个问题?

SELECT ArticleID, ArticleTitle, CoverImage, CreationDate, date_format(CreationDate, '%D %M %Y') AS 'Date', Visible 
FROM (SELECT a.ArticleID, a.ArticleTitle, a.CoverImage, a.CreationDate, null as Visible, a.GenreID, a.Score FROM articles AS a
JOIN article_game ON a.ArticleID = article_game.ArticleID
JOIN game ON article_game.GameID = game.GameID
JOIN genre ON a.GenreID = genre.GenreID
JOIN article_console ON a.ArticleID = article_console.ArticleID
JOIN console ON article_console.ConsoleID = console.ConsoleID
WHERE (
a.ArticleTitle LIKE '%".$searchText."%'
OR game.GameName LIKE '%".$searchText."%'
OR genre.Genre LIKE '%".$searchText."%'
OR console.Console LIKE '%".$searchText."%'
)
AND genre.GenreID = ".$G."
AND a.Score = ".$S."
UNION
SELECT ta.ArticleID, ta.ArticleTitle, ta.CoverImage, ta.CreationDate, ta.Visible, ta.GenreID, ta.Score FROM temp_article AS ta
JOIN temp_article_game ON ta.ArticleID = temp_article_game.ArticleID
JOIN game ON temp_article_game.GameID = game.GameID
JOIN genre ON ta.GenreID = genre.GenreID
JOIN temp_article_console ON ta.ArticleID = temp_article_console.ArticleID
JOIN console ON temp_article_console.ConsoleID = console.ConsoleID
WHERE (
ta.Visible = true AND ta.ArticleTitle LIKE '%".$searchText."%'
OR game.GameName LIKE '%".$searchText."%'
OR genre.Genre LIKE '%".$searchText."%'
OR console.Console LIKE '%".$searchText."%'
)
AND genre.GenreID = ".$G."
AND ta.Score = ".$S."
) a
ORDER BY CreationDate desc"

其中 $G 和 $S 分别与 $_POST['GenreID'] 和 $_POST['Score'] 中找到的内容相关联,两者都不必具有值,并且应该能够保留为,值为“”。 (目前这就是破坏该声明的原因)。

由于流派是一个ID,而分数是一个确定的整数,我不想使用like子句来解决这个问题,因为这可能会返回太多的值,例如“12”,“112”,“122”,“例如,123"等当您只寻找一种流派时,这是没有用的。

最佳答案

这个问题的答案是停止将 MySQL 查询视为查询,而开始将其视为字符串。以这种方式思考意味着我可以使用 if 语句连接字符串。这是解决我的问题的结果字符串集合。

$SearchQuery = "SELECT ArticleID, ArticleTitle, CoverImage, CreationDate, date_format(CreationDate, '%D %M %Y') AS 'Date', Visible 
FROM (SELECT a.ArticleID, a.ArticleTitle, a.CoverImage, a.CreationDate, null as Visible, a.GenreID, a.Score FROM articles AS a
LEFT JOIN article_game ON a.ArticleID = article_game.ArticleID
LEFT JOIN game ON article_game.GameID = game.GameID
LEFT JOIN genre ON a.GenreID = genre.GenreID
LEFT JOIN article_console ON a.ArticleID = article_console.ArticleID
LEFT JOIN console ON article_console.ConsoleID = console.ConsoleID
WHERE (
a.ArticleTitle LIKE '%".$searchText."%'
OR game.GameName LIKE '%".$searchText."%'
OR genre.Genre LIKE '%".$searchText."%'
OR console.Console LIKE '%".$searchText."%'
)";
if (!empty($G) && ($G != -1)){
$SearchQuery .= "AND genre.GenreID = ".$G." ";
}
if (!empty($S) && ($S != -1)){
$SearchQuery .= "AND a.Score = ".$S." ";
}

$SearchQuery .= "UNION
SELECT ta.ArticleID, ta.ArticleTitle, ta.CoverImage, ta.CreationDate, ta.Visible, ta.GenreID, ta.Score FROM temp_article AS ta
LEFT JOIN temp_article_game ON ta.ArticleID = temp_article_game.ArticleID
LEFT JOIN game ON temp_article_game.GameID = game.GameID
LEFT JOIN genre ON ta.GenreID = genre.GenreID
LEFT JOIN temp_article_console ON ta.ArticleID = temp_article_console.ArticleID
LEFT JOIN console ON temp_article_console.ConsoleID = console.ConsoleID
WHERE (
ta.Visible = true AND ta.ArticleTitle LIKE '%".$searchText."%'
OR game.GameName LIKE '%".$searchText."%'
OR genre.Genre LIKE '%".$searchText."%'
OR console.Console LIKE '%".$searchText."%'
)";
if (!empty($G) && ($G != -1)){
$SearchQuery .= "AND genre.GenreID = ".$G." ";
}
if (!empty($S) && ($S != -1)){
$SearchQuery .= "AND ta.Score = ".$S." ";
}
$SearchQuery .= ") a
ORDER BY CreationDate desc";

这给了我相同的结果,但如果 $G 或 $S 的值为 -1,则忽略它们。对相同值的 !empty 检查可确保查询始终有效,否则首次加载页面时将考虑帖子值,通过在其中包含“AND GenreID = AND a.Score =”来破坏查询,而没有要检查的值。

关于mysql - 如何在未输入任何内容时获取 MySQL 查询并返回所有结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29572309/

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