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

时间:2016-02-14 19:42:47

标签: php mysql

我有一个问题:

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

选择:

| 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                          |

来自我的桌子。

如何访问custom_field_value = custom_field_to_compare_to所在的3?我可以判断这是否存在吗?

我知道我可以修改查询并立即选择它,但我需要缩小已经执行的查询。

2 个答案:

答案 0 :(得分:1)

简单地再次查询数据库可能更有意义。

但是,如果您想搜索已经检索过的结果,可以使用例如array_filter()

一个简单的例子:

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

答案 1 :(得分:0)

最简单的解决方案是迭代结果集并找到具有目标值的行。

但是,如果您不想使用循环,可能需要查看PHP LINQ。有了这个,你可以这样做:

// 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'); 
相关问题