[英]Search query not working
I have coded a mysql query for multiple fields, and result should match all the fields. 我已经为多个字段编写了mysql查询代码,并且结果应该与所有字段匹配。 Below is my code,
下面是我的代码,
SELECT e.es_id, e.es_sex, e.service_type, e.working_name, d.es_age, d.es_city, d.es_regional_city
FROM escorts AS e
INNER JOIN escorts_details AS d ON e.es_id = d.es_id
WHERE es_sex LIKE '%" . $es_sex . "%'
AND category LIKE '%" . $category ."%'
AND es_city LIKE '%" . $es_city ."%'
AND es_regional_city LIKE '%" . $es_regional_city ."%'";
and when I m executing this code after filling all the fields.. 当我填写所有字段后执行此代码时..
SELECT e.es_id, e.es_sex, e.service_type, e.working_name, d.es_age, d.es_city, d.es_regional_city
FROM escorts AS e
INNER JOIN escorts_details AS d
ON e.es_id = d.es_id
WHERE es_sex LIKE '%male%'
AND category LIKE '%waitresses%'
AND es_city LIKE '%7%'
AND es_regional_city LIKE '%%'
Its still showing female details, if you will see the excuted query, i have filled "male" (es_sex LIKE '%male%'). 它仍然显示女性详细信息,如果您将看到被替换的查询,则我填写了“男性”(es_sex LIKE'%male%')。
I don't know what is I m doing wrong. 我不知道我在做什么错。
Please help me. 请帮我。
Thanks. 谢谢。
It's because % represents any string of characters. 这是因为%代表任何字符串。 So in your case LIKE %male% actually could mean 'female', where 'fe' is being put in place of the first %.
因此,在您的情况下,像%male%这样的词实际上可能表示'female',其中用'fe'代替前一个%。 Just remove % and say es_sex = 'male', like this:
只需删除%并说es_sex ='male',像这样:
SELECT e.es_id, e.es_sex, e.service_type, e.working_name, d.es_age, d.es_city, d.es_regional_city
FROM escorts AS e
INNER JOIN escorts_details AS d
ON e.es_id = d.es_id
WHERE es_sex ='male'
AND category LIKE '%waitresses%'
AND es_city LIKE '%7%'
AND es_regional_city LIKE '%%'
First you need to understand LIKE
and =
are not equal. 首先,您需要了解
LIKE
和=
不相等。 LIKE in this case (with % on both sides) returns true when the key is found anywhere in the specifics column. 如果在“详细信息”列中的任何位置找到了密钥,则在这种情况下,“ LIKE”(两边都有%)将返回true。 That is
%male%
can come true for the value fe male also as the word male is present in it. 也就是说,对于
%male%
fe male而言, %male%
也可以实现,因为其中包含单词male。
The %
symbol represents that there can be any character or group of characters. %
符号表示可以有任何字符或一组字符。
You have to use es_sex='male'
in this case. 在这种情况下,您必须使用
es_sex='male'
。
I believe you would face syntax error near where
condition. 我相信您将在
where
条件附近遇到语法错误。
try by select the table name along with '.'
尝试选择表名以及“。”。 leaded with field name (table-name.field-name) like given below
以字段名(table-name.field-name)开头,如下所示
SELECT e.es_id, e.es_sex, e.service_type, e.working_name, d.es_age, d.es_city, d.es_regional_city FROM escorts AS e INNER JOIN escorts_details AS d ON e.es_id = d.es_id WHERE e.es_sex LIKE '%male%' AND e.category LIKE '%waitresses%' AND e.es_city LIKE '%7%' AND e.es_regional_city LIKE '%%'
i have included escort table name as e.field-names, kindly refer the field name corresponding to the table name.
我已将护送表名称作为e.field-names包括在内,请参考与表名称相对应的字段名称。
I hope you will get the correct answer. 希望您能得到正确的答案。 give me your feedback.
给我您的反馈。 happy coding.
快乐的编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.