简体   繁体   English

WordPress 查询,meta_query,比较 BETWEEN,没有结果

[英]WordPress query, meta_query, compare BETWEEN, no results

I have code for my filter.我有我的过滤器的代码。 It worked well until I add new product in my database.它运行良好,直到我在我的数据库中添加新产品。 I found the problem, but dont know what to do with that.我发现了问题,但不知道该怎么办。

I have parameters "alc_min" and "alc_max" in my filter.我的过滤器中有参数“alc_min”和“alc_max”。 I get these from crawling all products.我从抓取所有产品中得到这些。 After I send this filter, I fire this code:发送此过滤器后,我触发此代码:

$meta_query = array();
$b = "alc_min";
$c = "alc_max";
if (isset ( $data [$b] ) && isset ( $data [$c] )) {
    $compare = "BETWEEN";
    $a = array (
            'key' => "alc",
            'value' => array (
                    $data [$b],
                    $data [$c]
            ),
            'compare' => $compare
    );
    array_push ( $meta_query, $a );
}

            $items = new WP_Query ( array (
                    'post_type' => $type,
                    'posts_per_page' => $posts_per_page,
                    'order' => $order,
                    'meta_key' => $orderkey,
                    'orderby' => $orderby,
                    'post_status' => 'publish',
                    'meta_query' => $meta_query,
                    'paged' => $paged
            ) );

Until now, it worked well.到目前为止,它运行良好。 No I add new product with "alc" <10 and I found, that if I have "alc_min" and "alc_max" <10 or >10, it is ok.不,我添加了“alc”<10 的新产品,我发现,如果我有“alc_min”和“alc_max”<10 或 >10,就可以了。 But if "alc_min" is <10 and "alc_max" >10 I get no results at all.但是,如果“alc_min”<10 且“alc_max”>10,我根本得不到任何结果。

Does anyone any idea what to check or fix?有谁知道要检查或修复什么?

After the clarification, I've suspected that the reason why selecting "alc_min" = 7 and "alc_max" = 13 doesn't yield any result is because of the column datatype.澄清后,我怀疑选择"alc_min" = 7 and "alc_max" = 13没有产生任何结果的原因是因为列数据类型。 Consider this example:考虑这个例子:

CREATE TABLE table1 (
alc VARCHAR(50));

INSERT INTO table1 VALUES 
('7'),
('9'),
('11'),
('13');

The table above is created with alc column datatype as VARCHAR instead of INTEGER (or numeric datatype).上表是使用alc列数据类型作为VARCHAR而不是INTEGER (或数字数据类型)创建的。 I've tested that running either one of the query below:我已经测试过运行以下任一查询:

SELECT * FROM table1 WHERE alc BETWEEN '7' AND '9';
SELECT * FROM table1 WHERE alc BETWEEN '11' AND '13';

will return the expected result.将返回预期的结果。 However, with this query:但是,使用此查询:

SELECT * FROM table1 WHERE alc BETWEEN '7' AND '13';

yields no result.没有结果。 This is because the values are treated as string instead of numbers and when that happens, 1 is always smaller than 7 .这是因为这些值被视为字符串而不是数字,当发生这种情况时, 1总是小于7 See below what happen you run select query with order by on the data set above:看看下面你在上面的数据集上运行 select query with order by 会发生什么:

SELECT * FROM table1 ORDER BY alc;

+-----+
| alc |
+-----+
|  11 |
|  13 |
|  7  |
|  9  |
+-----+

As you can see, since the data is treated as string (according to the column datatype), then you could imagine this in alphabetical form as the following:如您所见,由于数据被视为字符串(根据列数据类型),因此您可以按字母顺序将其想象为如下所示:

+-----+--------------+
| alc | alphabetical |
+-----+--------------+
|  11 |      AA      |
|  13 |      AC      |
|  7  |       G      |
|  9  |       I      |
+-----+--------------+

So, the condition of BETWEEN '7' AND '13' becomes BETWEEN 'G' AND 'AC' ;因此, BETWEEN '7' AND '13'变为BETWEEN 'G' AND 'AC' which doesn't really make sense.这真的没有意义。 And if you change to BETWEEN '11' AND '9' you'll get the correct result but that made the query even more confusing and not making sense at all.如果您更改为BETWEEN '11' AND '9'您将获得正确的结果,但这使查询更加混乱并且根本没有意义。

Now, I've discovered that there are at least 3 workaround/solution for this:现在,我发现至少有 3 个解决方法/解决方案:

  1. One of the oldest way I can think of is by adding +0 to the column in the query.我能想到的最古老的方法之一是在查询的列中添加+0 I didn't find any official docs about this but I assume that doing this will change the data value to numeric in the query:我没有找到任何关于此的官方文档,但我认为这样做会将查询中的数据值更改为数字:
         SELECT * FROM table1 
            WHERE alc+0 BETWEEN '7' AND '13';
  1. This is probably the same as above is just that I'm not sure if this is version specific or not.这可能与上面相同,只是我不确定这是否是特定于版本的。 It turns out that in my testing, if you didn't wrap the searched value in quotes, you'll get the result as if the data is numeric:事实证明,在我的测试中,如果您没有将搜索到的值用引号括起来,您将得到的结果就像数据是数字一样:
         SELECT * FROM table1 
            WHERE alc BETWEEN 7 AND 13;
  1. This require a change of column datatype but afterwards any of the query with or without quotes on the searched value should work:这需要更改列数据类型,但之后任何对搜索值带或不带引号的查询都应该起作用:
         ALTER TABLE table1 CHANGE alc alc INT;

I hope that this is true and the issue is really about column datatype.我希望这是真的,并且问题确实与列数据类型有关。 As far as I know, this is the closest thing to what your situation is that I had experience with.据我所知,这是我经历过的最接近你的情况的事情。

Here's a fiddle for reference 这是一个小提琴供参考

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

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