[英]mySQL - how to code SELECT statement correctly
I have a mySQL SELECT
statement with a combination of full text search and normal search in the WHERE
condition and an additional JOIN
like this 我有一个mySQL
SELECT
语句,在WHERE
条件下结合了全文搜索和普通搜索,还有一个像这样的附加JOIN
SELECT
customer.*,
countries.name as country
FROM customer
LEFT JOIN countries ON countries.ID = customer.country_id
WHERE customer.num = :keyword
OR customer.city = :keyword
OR customer.email = :keyword
OR MATCH (customer.company) AGAINST (:keyword)
OR countries.name = :keyword
Queries with a full text search into (customer.company)
column returns no result
as long as the table.column countries.name
is part of the WHERE
condition. 只要table.column
countries.name
是WHERE
条件的一部分,使用全文搜索到(customer.company)
列的查询就no result
返回no result
。 But queries into countries.name
itself are successful. 但是对
countries.name
本身的查询是成功的。
How to code the SELECT statement correctly to return a successful query using a combination of all WHERE components from above example ? 如何使用上面示例中的所有WHERE组件的组合正确编码SELECT语句以返回成功的查询?
EDIT : 编辑:
In a previous version I used this statment 在之前的版本中,我使用了这个声明
SELECT
customer.* ,
countries.name
FROM customer, countries
WHERE customer.country_id=countries.ID
AND MATCH (customer.company) AGAINST (:keyword)
UNION
SELECT
customer.*,
countries.name
FROM customer, countries
WHERE customer.country_id=countries.ID
AND countries.name=:keyword
which works well. 效果很好。 I just do not know if this is the efficient way to query two tables with different searchs (full text and normal).
我只是不知道这是否是使用不同搜索(全文和普通)查询两个表的有效方法。 Also when I search in more than 2 columns the code is getting blown up easily, which I would like to avoid.
此外,当我搜索超过2列时,代码很容易被炸毁,我想避免。
Any more ideas and help is welcome 欢迎任何更多的想法和帮助
You should check for NULLs
since you are using a LEFT JOIN
: 您应该检查
NULLs
因为您正在使用LEFT JOIN
:
SELECT
customer.*,
countries.name as country
FROM customer
LEFT JOIN countries ON countries.ID = customer.country_id
WHERE customer.num = :keyword
OR customer.city = :keyword
OR customer.email = :keyword
OR MATCH (customer.company) AGAINST (:keyword)
OR (countries.name = :keyword OR countries.name IS NULL)
Just for clarification, conditions on the right table of a LEFT JOIN
should be placed only inside the ON
clause, not the WHERE
clause . 只是为了澄清,
LEFT JOIN
的右表上的条件应该只放在ON
子句内,而不是WHERE
子句。 This is a bit different, since you want to compare it against all the other conditions, therefore - the NULL
comparison. 这有点不同,因为您要将其与所有其他条件进行比较,因此 -
NULL
比较。
My solution is just set a default value = ''
to countries.name
if it's null using CASE WHEN
我的解决方案只是设置一个默认值=
''
到countries.name
如果它使用CASE WHEN
null
(CASE WHEN countries.name is null then 'default value' else countries.name END)
new query 新查询
SELECT
customer.*,
countries.name as country
FROM customer
LEFT JOIN countries ON countries.ID = customer.country_id
WHERE customer.num = :keyword
OR customer.city = :keyword
OR customer.email = :keyword
OR MATCH (customer.company) AGAINST (:keyword)
OR (CASE WHEN countries.name is null then '' else countries.name END) = :keyword
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.