我的 SQL 搜索查询无法区分列属性?
我的数据库有两个单独的查询,它们返回两种不同类型数据的搜索结果,这些数据分别显示在搜索结果页面上。
显示的两个组是“联系人”和“客户”。客户是数据库中的联系人,其联系号码非为零!
这是一个普通查询,在查询中不使用任何 LIKE 语句。
$contacts = $db->query("SELECT DISTINCT contact.id, first_name, second_name, company_name, email FROM contact INNER JOIN contact_quote WHERE customer_id = 0 GROUP BY first_name, second_name, company_name, email, quote_name ");
$customers = $db->query("SELECT DISTINCT contact.id, first_name, second_name, company_name, email FROM contact INNER JOIN contact_quote WHERE customer_id != 0 GROUP BY first_name, second_name, company_name, email, quote_name ");
它工作正常并将结果显示到正确的类别中。
就像这样
但是 我的新查询是这样的
$contacts = $db->query("SELECT DISTINCT contact.id, first_name, second_name, company_name, email FROM contact INNER JOIN contact_quote WHERE customer_id =0 AND first_name LIKE '%$search%' OR second_name LIKE '%$search%' OR company_name LIKE '%$search%' OR email LIKE '%$search%' OR quote_name LIKE '%$search%' AND contact.id = contact_quote.cid GROUP BY first_name, second_name, company_name, email, quote_name");
$customers = $db->query("SELECT DISTINCT contact.id, first_name, second_name, company_name, email FROM contact INNER JOIN contact_quote WHERE customer_id !=0 AND first_name LIKE '%$search%' OR second_name LIKE '%$search%' OR company_name LIKE '%$search%' OR email LIKE '%$search%' OR quote_name LIKE '%$search%' AND contact.id = contact_quote.cid GROUP BY first_name, second_name, company_name, email, quote_name");
显示两个列表中的所有结果即使他们不是客户
屏幕截图:
我猜有问题在新查询中,它是如果“customer_id”列不等于零并且显示所有内容,则无法正确比较?
总而言之,我想这样做:
- 与搜索字符串比较
- 如果有任何联系人与搜索字符串匹配,请将它们放入联系人查询结果中
- 如果有任何客户与搜索字符串匹配,请将它们放入客户查询结果中。
谢谢
I have two seperate queries for my database which return search results for two different types of data which are displayed seperately on the search results page.
The two groups displayed are "contacts" and "customers". Customers are contacts in the database that have been given a contact number that is NOT zero!
This is a normal query which does not use any LIKE statements within the query.
$contacts = $db->query("SELECT DISTINCT contact.id, first_name, second_name, company_name, email FROM contact INNER JOIN contact_quote WHERE customer_id = 0 GROUP BY first_name, second_name, company_name, email, quote_name ");
$customers = $db->query("SELECT DISTINCT contact.id, first_name, second_name, company_name, email FROM contact INNER JOIN contact_quote WHERE customer_id != 0 GROUP BY first_name, second_name, company_name, email, quote_name ");
It works fine and displays results into their correct categories.
Like so
However My new query which is this
$contacts = $db->query("SELECT DISTINCT contact.id, first_name, second_name, company_name, email FROM contact INNER JOIN contact_quote WHERE customer_id =0 AND first_name LIKE '%$search%' OR second_name LIKE '%$search%' OR company_name LIKE '%$search%' OR email LIKE '%$search%' OR quote_name LIKE '%$search%' AND contact.id = contact_quote.cid GROUP BY first_name, second_name, company_name, email, quote_name");
$customers = $db->query("SELECT DISTINCT contact.id, first_name, second_name, company_name, email FROM contact INNER JOIN contact_quote WHERE customer_id !=0 AND first_name LIKE '%$search%' OR second_name LIKE '%$search%' OR company_name LIKE '%$search%' OR email LIKE '%$search%' OR quote_name LIKE '%$search%' AND contact.id = contact_quote.cid GROUP BY first_name, second_name, company_name, email, quote_name");
Displays all results in both lists even if they are NOT A Customer
Screenshot:
Im guessing there is something wrong in the new query in which It is not able to compare correctly if the column "customer_id" is not equal to zero and is displaying everything?
To summarise I want to do this:
- Compare with search string
- If any contacts match the search string put them in the contacts query result
- If any customers match the search string put them in the customers query result.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 LIKE 语句需要加上括号。它会进入您的
WHERE
子句,检查customer_id=0 AND first_name LIKE '%$search%'
,然后检查OR secondary_name LIKE '%$ search'
,如果customer_id=0
也没关系,因为它现在只检查一种情况。通过在其周围添加括号,它将检查customer_id=0
,然后检查所有的“喜欢”。You need parenthesis around your LIKE statements. It's getting to your
WHERE
clause, checks forcustomer_id=0 AND first_name LIKE '%$search%'
and then the next it checksOR second_name LIKE '%$search'
and it won't matter ifcustomer_id=0
because it's only checking the one case now. By putting the parens around it, it will checkcustomer_id=0
and then all the LIKES.您需要使用括号,因为名字,姓氏,电子邮件,引用名称都必须与搜索进行比较,然后与 id 进行 AND 操作,否则 quote_name 与搜索进行比较,并与 id 进行 AND 运算,其他则进行 OR 运算。
you need to use a parenthesis because the first_name,last_name,email,quote_name all have to be to compare with search and then do an AND with the id otherwise quote_name is compared with search and ANDed with the id and others are ORed.