简体   繁体   English

编写Postgresql查询的问题

[英]Issue in writing postgresql query

I am new to Postgresql.I need to write a search query where i have array of values which is to be searched in a column that holds array.How can i search that atleast one value from my search array is present in the column array. 我是Postgresql的新手,我需要编写一个搜索查询,其中有一个值数组,要在保存数组的列中进行搜索。如何从我的搜索数组中搜索至少一个值存在于列数组中。

this is my array which is to be searched, 这是我要搜索的数组,

Array
(
[0] => 24
[1] => 25
[2] => 26
)

& my table w_business has a column categories that contains values as: &我的表w_business的列类别包含以下值:

["23","45","24"]
["11","34"]

ho can i write query for searching in POSTGRESQL? 我如何编写查询以在POSTGRESQL中搜索?

Your best chance at solving the immediate problem is probably a PHP function that returns part of a well-formatted WHERE clause. 解决当前问题的最佳机会可能是一个PHP函数,该函数返回格式正确的WHERE子句的一部分。 But you have a lot more trouble coming up. 但是您遇到了很多麻烦。 The very best use of your time right now is in getting that table fixed. 您现在最好的时间就是修复该表。

Having said that, let's create a table and some sample rows. 话虽如此,让我们创建一个表和一些示例行。

create temp table scratch (
  scratch_id integer primary key,
  categories text
);

insert into scratch values (1, '24, 35, 36'); -- Well-formed match
insert into scratch values (2, '23, 24, 35'); -- Well-formed match
insert into scratch values (3, '33, 34, 24'); -- Well-formed match
insert into scratch values (4, '24,35,36'); -- Ill-formed match; no spaces
insert into scratch values (5, '23,24,36'); -- Ill-formed match; no spaces
insert into scratch values (6, '22,23,24'); -- Ill-formed match; no spaces
insert into scratch values (7, '24 , 35 , 36'); -- Ill-formed match
insert into scratch values (8, '23 , 24 , 35'); -- Ill-formed match
insert into scratch values (9, '23 , 24  '); -- Ill-formed match
insert into scratch values (10, '  24 ,34  '); -- Ill-formed match

insert into scratch values (100, '2424'); -- Well-formed fail
insert into scratch values (101, '1224, 241');-- Well-formed fail

And a PHP function that returns a well-formatted WHERE clause. 还有一个PHP函数,它返回格式正确的WHERE子句。 (Well-formatted as far as SQL is concerned, that is.) This builds a WHERE clause on the fly. (就SQL而言,格式是正确的。)这将动态地构建WHERE子句。 ( See this article for why you shouldn't do this.) How well this works depends on how consistent your data is. 有关为什么不应执行此操作的信息, 请参阅本文 。)这种方法的效果取决于数据的一致性。 Since the database designer has thrown type safety to the winds, you'll need some luck here. 由于数据库设计人员一头雾水,所以这里需要运气。 (Without type safety, you're liable to find values like "24, 25i, wibble" in there.) (没有类型安全性,您可能会在其中找到“ 24、25i,摆动”之类的值。)

function where_clause_for_categories($arr) {
  $categories = [];
  $patterns = array (
               "begin" => '^:val:[, ]', 
               "middle" => '[, ]:val:[, ]', 
               "end" => '[, ]:val:$'
              );
  $full_pattern = "";
  foreach ($arr as $value) {
      foreach ($patterns as $pattern) {
        $full_pattern = str_replace(":val:", $value, $pattern);
        array_push($categories, "categories ~ '" . $full_pattern . "'");
      }
  }
  $where = implode(' or ', $categories);

  if (strlen($where) > 0) {
    $where = '(' . $where . ')';    
  }
  return $where;
}

A quick test . 快速测试。 . .

$arr = [24, 25];
echo where_clause_for_categories($arr);
// (categories ~ '^24[, ]' or categories ~ '[, ]24[, ]' or categories ~ '[, ]24$'
 or categories ~ '^25[, ]' or categories ~ '[, ]25[, ]' or categories ~ '[, ]25$')

The outer parens guard against side effects when you combine this with other parts of a WHERE clause. 当您将其与WHERE子句的其他部分结合使用时,外部parens可以防止产生副作用。

Assuming you won't pass an empty array to where_clause_for_categories(), you can change the last part of your code to this. 假设您不会将空数组传递给where_clause_for_categories(),则可以将代码的最后一部分更改为此。

echo $qry="SELECT * FROM w_business WHERE enabled='t' AND " . where_clause_for_categories($arr);

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

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