简体   繁体   English

具有多种条件但结果有限的MySQL查询

[英]MySQL Query with multiple conditions but limited results

I have a PHP script that creates an RSS feed from our MySQL database. 我有一个PHP脚本,可从我们的MySQL数据库创建RSS提要。 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. 我一直在更改MySQL查询,在这里搜索以解决问题后,至少现在返回了结果,但是无论我为查询中的记录数设置了多少限制,它都只能带来几个月的结果。

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. $ news_number设置为200,但我尝试使用更高的数字进行操作,结果数量未发生变化。

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. 因此,举个例子,如果我在上面运行查询,我会得到许多结果,但少于限制,它会在11月初停止显示。

However if I do a LIKE search using just one term ie blue then it brings back more results going back over a year. 但是,如果我仅使用一个词(例如蓝色)进行LIKE搜索,则可以返回一年以上的更多结果。

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. 我的问题是MySQL查询看起来是否正确-已经尝试了很多事情,但是尽管花费了很多时间却似乎无法正确执行。

Thanks in advance 提前致谢

ADDED: 添加:

So to clarify if I run this query as an example I get 200 results returned: 因此,澄清一下是否以该查询为例,我得到了200条结果:

$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: 但是,如果我在OR部分中使用红色运行此查询,则将得到35个结果:

$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?? 在第二个代码中,除非代码错误,否则我期望得到200多个结果?

Thanks to those making suggestions - have updated results below: 多亏了这些建议-更新了以下结果:

To test as mentioned by flynorc I changed the query so it read like this: 为了测试flynorc所提到的,我更改了查询,使其显示如下:

("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. 结果是它返回的结果数量与我的相同(上面的原始第一个结果),并且返回的结果在11月初停止。

So I removed the LIMIT $news_number and the number of results did not change. 因此,我删除了LIMIT $ news_number,结果数没有变化。

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. 随着记录在今年3月停止,它带来了更多的结果-尽管它的记录可以追溯到3年,但仍然不是整个数据库。

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. 查询中的问题是弯括号,您还需要在其他查询中为LIKE条件添加它们。

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. 因此,在使用LIKE条件将其与sql查询中的其他条件隔离时,我们需要使用弯括号。

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 sql小提琴:: http://sqlfiddle.com/#!9/d262b8/2

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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