gpt4 book ai didi

mysql - 如何在 MySQL 中使用 AND 和 OR

转载 作者:行者123 更新时间:2023-11-29 05:54:57 26 4
gpt4 key购买 nike

我想在我的网站上创建一个搜索。在搜索表单中,我有用于搜索的类别和输入查询。例如,在类别 im 中选择 jmovies 并在输入查询中为 ashi 键入关键字。

我的代码

SELECT * 
FROM master_post
WHERE category = 'jmovies'
AND master_post_name LIKE '%ashi%'
OR altname LIKE '%ashi%'

因此类别 = jmovies 和关键字 = ashi

例如我有这样的表

category     master_post_name    alt_name
=========== ================== ===============
JMovies ashi_include_1 not_include
JMovies ashi_include_2 not_include
JMovies ashi_include_3 not_include
JMovies not_include ashi_include_1
JMovies not_include not_include
Drama ashi_in_drama ashi_include_2

如果我用那个代码运行结果是

category     master_post_name    alt_name
=========== ================== ===============
JMovies ashi_include_1 not_include
JMovies ashi_include_2 not_include
JMovies ashi_include_3 not_include
JMovies not_include ashi_include_1
Drama ashi_in_drama ashi_include_2

问题是,因为我选择了jmovies,类别drama 不应该出现在我的结果中,即使drama 在 中有ashi>master_postalt_name

结果一定是这样的

category     master_post_name    alt_name
=========== ================== ===============
JMovies ashi_include_1 not_include
JMovies ashi_include_2 not_include
JMovies ashi_include_3 not_include
JMovies not_include ashi_include_1

即使我选择 jmovies 而不是填写 input keyword 结果也不仅仅显示 jmovies 但它也显示 drama

注意:alt_namema​​ster_post_name 具有相同的关键字。

最佳答案

我不确定我是否正确理解你的问题,但你可以尝试以下查询:

SELECT * FROM master_post 
WHERE category = 'jmovies' AND (master_post_name LIKE '%ashi%' OR altname LIKE '%ashi%')

我怀疑这里的“问题”在于 AND 运算符的优先级高于 OR 运算符。

关于mysql - 如何在 MySQL 中使用 AND 和 OR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50308039/

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