简体   繁体   English

从查询中按值查找特定字段

[英]Find specific field by value from query

I have a query: 我有一个查询:

SELECT * FROM custom_fields_values WHERE `custom_field_value_for_item-id` = 1

which selects: 选择:

| custom_field_value_id | custom_field_for_item-id | custom_field_value | custom_field_to-compare_to |
|-----------------------|--------------------------|--------------------|----------------------------|
| 1                     | 1                        | test1              | 3                          |
| 2                     | 1                        | test2              | 4                          |

from my table. 从我的桌子上。

How can I access the custom_field_value where custom_field_to_compare_to == 3 ? 如何在custom_field_to_compare_to == 3访问custom_field_value And can I tell if this doesn't exist? 我能说出这是否不存在吗?

I know I could modify the query and just select it straight away, but I need to narrow down the already performed query. 我知道我可以修改查询并直接选择它,但是我需要缩小已经执行的查询的范围。

It would probably make more sense to simply query the database again. 再次简单地查询数据库可能更有意义。

But if you want to search through the results that you already have retrieved, you could use for example array_filter() . 但是,如果要搜索已经检索到的结果,则可以使用例如array_filter()

A simple example: 一个简单的例子:

$search = "3";
$filtered_rows = array_filter($all_found_rows, function($value) use ($search) {
    return $Value['custom_field_to-compare_to'] === $search;
  }
);

The easiest solution will be to just iterate over the resultset and find the row with the target value. 最简单的解决方案是仅遍历结果集并找到具有目标值的行。

However, if you do not wish to use loops, you may want to look into PHP LINQ . 但是,如果您不想使用循环,则可能需要研究PHP LINQ With that you can do something like this: 这样,您可以执行以下操作:

// Assuming $result has the resultset from the query
$data= from('$result')->in($result)
            ->where('$result=> $result->custom_field_to_compare_to == 3')
            ->select('$result->custom_field_value'); 

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

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