mysql和或查询 [英] mysql and or query
问题描述
我需要进行搜索查询,用户可以按名称,课程,年份或成员,以上任意一项进行搜索,并且用户可以使用所有字段或任意多个字段进行搜索,例如;-仅与名称&当然.
I need to make a search query, where a user can search by name or course or year or member, any one of above and also the user cam make a search with all the field or any number of field, e.g.;-as only with name & course.
这怎么可能?我不想进行其他查询,单个查询可以吗?现在我的查询是:
How it is possible? I don't want to make different query, can it be possible with a single query? now my query is:
$query="SELECT * FROM fsb_profile
WHERE profile_name = '".$_REQUEST['name']."'
AND profile_member= '".$_REQUEST['type']."'
AND profile_year= '".$_REQUEST['year']."'
AND profile_course='".$_REQUEST['course']."'
OR profile_name = '".$_REQUEST['name']."'
OR profile_member= '".$_REQUEST['type']."'
OR profile_year= '".$_REQUEST['year']."'
OR profile_course='".$_REQUEST['course']."'";
但是它没有运行,它仅在任何一个字段中搜索,怎么可能?
but it's not running, it's only searching by any one of the field, how it is possible?
推荐答案
如果要在where子句中将AND与OR混合使用,则使用方括号来准确指示组合的表达方式非常有用:(A AND B )OR C与A AND(B OR C)不同. SQL将以自己的方式解释A AND B OR C(不带方括号),但不一定解释您期望(或打算)如何解释它.
If you're mixing AND with OR in a where clause, then it's useful to use brackets to indicate exactly how the combination should be expressed: (A AND B) OR C is not the same as A AND (B OR C). SQL will interpret A AND B OR C (without any brackets) in its own manner, but not necessarily how you might expect (or intend) it to be interpreted.
例如
WHERE Likes Soccer AND ( Likes Pizza OR Likes Whiskey )
正在寻找一个喜欢足球,喜欢比萨饼或威士忌的人;因此会选择喜欢比萨的足球爱好者,喜欢威士忌的足球爱好者,喜欢比萨饼和威士忌的足球爱好者
is looking for a person who likes Soccer and likes either Pizza or Whiskey; so it will pick Soccer-lovers who like Pizza, Soccer-lovers who like Whiskey, Soccer-lovers who like both Pizza and Whiskey
WHERE ( Likes Soccer AND Likes Pizza ) OR Likes Whiskey
正在寻找既喜欢足球又喜欢披萨的人,或者喜欢威士忌的人.因此它将选择必须也喜欢比萨饼的足球爱好者或威士忌爱好者,或必须也喜欢比萨饼但也可能喜欢威士忌的足球爱好者
is looking for a person who likes both Soccer and Pizza, or a person who likes Whiskey; so it will pick Soccer-lovers who must also like Pizza, or Whiskey-lovers, or Soccer-lovers who must also like Pizza but may also like Whiskey
这篇关于mysql和或查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!