简体   繁体   中英

MySQL Query with multiple conditions but limited results

I have a PHP script that creates an RSS feed from our MySQL database. The records in the database cover a three year period.

I have been altering the MySQL query and after searching here to fix issues then there is at least now results being returned but it only brings back a few months of results regardless of the limit I set for the number of records in the query.

Here is the query:

$mysqli->query("
SELECT * 
  FROM news 
 WHERE (`title` LIKE '%red%' 
      OR `title` LIKE '%green%' 
      OR `title` LIKE '%blue%' 
      OR `title` LIKE '%yellow%' 
      OR `title` LIKE '%black%') 
   AND published = 1
   AND category_id != 9
   AND category_id != 10
 ORDER 
    BY id DESC LIMIT $news_number")

$news_number is set as 200 but i've tried it with higher numbers and the number of results don't change.

So as an example if I run the query above I get a number of results but less than the limit and it stops say at the start of November.

However if I do a LIKE search using just one term ie blue then it brings back more results going back over a year.

My question is does the MySQL query look correct - have tried many things but can't seem to get it right despite spending quite a bit of time.

Thanks in advance

ADDED:

So to clarify if I run this query as an example I get 200 results returned:

$mysqli->query("SELECT * FROM news WHERE `title` LIKE '%red%' AND
    published='1' AND category_id !='9' AND category_id !='10'
    ORDER BY id DESC LIMIT $news_number")

But If I run this query with red included in the OR part then I get 35 results:

$mysqli->query("SELECT * FROM news WHERE (`title` LIKE '%red%' OR `title` LIKE '%green%' OR
    `title` LIKE '%blue%' OR `title` LIKE '%yellow%' OR `title` LIKE '%black%') AND
    published='1' AND category_id !='9' AND category_id !='10'
    ORDER BY id DESC LIMIT $news_number")

On the second one I would expect to get well over 200 results unless the code is wrong??

Thanks to those making suggestions - have updated results below:

To test as mentioned by flynorc I changed the query so it read like this:

("SELECT * FROM news WHERE title REGEXP 'red|green|blue|yellow|black' AND published='1' AND category_id !='9' AND category_id !='10' ORDER BY id DESC  LIMIT $news_number")

The results were that it returned the same number of results as mine (the original first one above) and the returned results stopped start of November.

So I removed the LIMIT $news_number and the number of results did not change.

So then I changed the query again to read like this:

("SELECT * FROM news WHERE title REGEXP 'red|green' AND published='1' AND category_id !='9' AND category_id !='10' ORDER BY id DESC")

It brought back many more results with the records stopping in March this year - still not the whole database though as it has records going back three years.

To clarify on the title field - this contains titles that have complete words that I am trying to match and there are plenty of titles that should match. What I mean is i'm not really looking for words like reddish I just want to match it with words like red

Apologies if my explanations are not brilliant - this has got me really confused. At this stage the only way I can see to get more results would be to run two scripts with the query broken down into a shorter ones.

The issue in your query is the curved braces, you need to add them for LIKE conditions in your other queries as well.

Below two queries look similar, but look close i added curved braces () like you for one of them, because of this they will produce different output. 2nd one is correct.

So we need to use curved braces when using LIKE conditions to isolate them from other conditions in an sql query.

SELECT * FROM test WHERE str LIKE '%red%' OR  str LIKE '%green%' OR str LIKE '%blue%' OR str LIKE '%yellow%' OR str LIKE '%black%' AND id=1;


SELECT * FROM test WHERE (str LIKE '%red%' OR  str LIKE '%green%' OR str LIKE '%blue%' OR str LIKE '%yellow%' OR str LIKE '%black%') AND id=1;

sql fiddle :: http://sqlfiddle.com/#!9/d262b8/2

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM