Using MySQL 5.6, this request
SELECT foo
FROM bar
WHERE groupId = '1'
AND MATCH (foo) AGAINST ('"myQuery"' IN BOOLEAN MODE);
and
SELECT foo
FROM bar
WHERE groupId = '1'
AND foo like '%myQuery%';
returns both correct results, but when I combine the two with:
SELECT foo
FROM bar
WHERE groupId = '1'
AND (
MATCH (foo) AGAINST ('"myQuery"' IN BOOLEAN MODE)
OR foo LIKE '%myQuery%'
);
I got some extra results, which do not appear in any of the first two requests, and didn't contain myQuery
at all.
Is there any trick with the parenthesis that I missed?
Or could it be related to any sort of index cache? Sometimes the results are correct, and suddenly, there are not anymore.
I also tried with
WHERE (
groupId = '1' AND MATCH (foo) AGAINST ('"myQuery"' IN BOOLEAN MODE)
) OR (
groupId = '1' AND foo like '%myQuery%'
);
Edit: here are the results of my requests, with myQuery = 'gold'
.
The 1st and 2nd one returns:
'Fancy gold'
'Nice gold'
'Super Nice gold'
'Ugly gold'
The last one returns:
'Cornices diamond'
'Custom'
'Fancy gold'
'Nice gold'
'Super Nice gold'
'Ugly gold'
One other thing I noticed, I ran Optimize table bar
, and then results are correct. I ran again 1st request again, and then the 3rd results are not correct anymore. So I really suspect something related to the full-text index.
Edit 2: Here is a dbFiddle : https://www.db-fiddle.com/f/iSXdTK7EzfoQ46RgDX7wF3/1
OR
. Personally, I won't use it, I rather useUNION
orUNION ALL
. I try very much avoid usingOR
. – MainmastOR
works perfectly fine in mySql, don't blame a tool if you can't use it properly, and please don't try to persuade people to use other tools that are not the same thing at all – AllembracingUNION
returns correct values yes. But instead of blindly use it, I would like to understand why is theOR
one is so weird. Furthermore, theUNION
version is more expensive – SontichOR
in mySql, why not ask a question like the OP? For the question here, examples of data and unexpected results would be welcome – AllembracingOR
. I didn't asks about the strange results I got while usingOR
because I've search in SO in find a question discussing about the difference betweenOR
andUNION
. The accepted answer said thatOR
is equivalent toIN
and when I appliedIN
it solved my issue. – MainmastSHOW CREATE TABLE
. And sample rows, if practical. – RedneckOR
clause. Check the result of third query in this fiddle: db-fiddle.com/f/hivf5CnJnbbGtXB8oWLhLN/9 – DespairOR
, at least not now but I'll appreciate the information and I will test it on our working servers just to make sure. We're not using MySQL 5.xx in any of our servers at the moment but I'm curious to know if this bug appear in MariaDB, hopefully not. – MainmastOR
andUNION
. So switching toUNION
might be the best workaround. – RedneckOR
?" So basically the answer is the MySQL version has a bug. If like that, there's no other solution than to use a workaround specifically for this MySQL version. Another solution without a workaround is to upgrade as suggested and tested by Madhur Bhaiya but as Rick James point out that this behavior also present in 8.0, it restricted the upgrade solution to maybe only v5.7. – MainmastMATCH
succeeds, theLIKE
will then match to any string if the LIKE uses the same column. – Becquerel