【问题标题】:Use "like" operator and OR clause使用“like”运算符和 OR 子句
【发布时间】:2018-01-31 05:18:19
【问题描述】:

我一直在寻找解决问题的方法,但没有找到答案。 我的数据库中有这些数据

{
  "_items": [
    {
      "_id": "5a69bbbe085eb50006b7515b",
      "sku": "SAB-EM",
      "name": "Sabão em pó",
      "price": 12,
      "promotions": [],
      "_updated": "2018-01-25T11:13:02.000000Z",
      "_created": "2018-01-25T11:13:02.000000Z",
      "_etag": "49fda82778b967e008ba46f7600822e940abb92b",
      "_links": {
        "self": {
          "title": "Product",
          "href": "products/5a69bbbe085eb50006b7515b"
        }
      }
    },
    {
      "_id": "5a69bbc7085eb50006b7515c",
      "sku": "AMA",
      "name": "Amaciante",
      "price": 15,
      "promotions": [
        {
          "bundle_quantity": 10,
          "paid_quantity": 8,
          "_cls": "QuantityBundlePromotion"
        },
        {
          "bundle_quantity": 3,
          "paid_quantity": 2,
          "_cls": "QuantityBundlePromotion"
        }
      ],
      "_updated": "2018-01-25T11:13:11.000000Z",
      "_created": "2018-01-25T11:13:11.000000Z",
      "_etag": "2308eb7c8c597ee441229d593a545e7e61a30160",
      "_links": {
        "self": {
          "title": "Product",
          "href": "products/5a69bbc7085eb50006b7515c"
        }
      }
    },
    {
      "_id": "5a69bbca085eb50006b7515d",
      "sku": "DET",
      "name": "Detergente",
      "price": 2,
      "promotions": [
        {
          "minimum_quantity": 5,
          "discount_percentage": 0.15,
          "_cls": "DiscountForMinimumQuantityPromotion"
        }
      ],
      "_updated": "2018-01-25T11:13:14.000000Z",
      "_created": "2018-01-25T11:13:14.000000Z",
      "_etag": "73583c72a90bc8b89e30d9b7ab5b904cd78827a1",
      "_links": {
        "self": {
          "title": "Product",
          "href": "products/5a69bbca085eb50006b7515d"
        }
      }
    }
  ],
  "_links": {
    "parent": {
      "title": "home",
      "href": "/"
    },
    "self": {
      "title": "products",
      "href": "products?max_results=3"
    },
    "next": {
      "title": "next page",
      "href": "products?max_results=3&page=2"
    },
    "last": {
      "title": "last page",
      "href": "products?max_results=3&page=29"
    }
  },
  "_meta": {
    "page": 1,
    "max_results": 3,
    "total": 87
  }
}

当我使用http://0.0.0.0:3000/products 搜索时,这就是结果。 有我的问题,我需要使用 like 运算符按 namesku 进行搜索。但是我到处找,什么也没找到,我试过使用 MongoDB 格式,但它返回 400。

当我运行 http://0.0.0.0:3000/products?where=name=="SAB-EM" or sku=="SAB-EM" 时,它会返回一些内容,但我仍然需要使用 like 运算符来执行此操作,而不是文字。

PS:我无权访问 python-eve 配置。

Tks.

【问题讨论】:

    标签: eve


    【解决方案1】:

    您可能想逃避您的查询。试试这样的:

    http://localhost:5000/regions?where=name==SAB-EM%20or%20sku==es
    

    【讨论】:

    • 如果它解决了您的问题,请接受答案,以便其他人知道这是一个有效的解决方案。
    • 这已经可以了,我确实需要使用like LIKE 运算符,但我没有找到如何制作它。 SQL 查询示例 Select * from products where name like("SAB%") or sku like ("SAB%")
    • 哦,我明白了。然后看看这个答案是否有帮助(但您很可能需要访问 eve 配置才能从黑名单中删除正则表达式):stackoverflow.com/questions/23097154/python-eve-contains-filter/…
    • 这个我试过了,不行。那个结果是测试,他们开发了后端和eve server,我只能访问数据。感谢您的帮助。
    【解决方案2】:

    我知道这个问题很老了,但是: 如果您将 python-eve 与 SQL (sqlalchemy) 一起使用,则语句 'or' 和 'and' 与 'like' 是:

    /resource?where={"or_": [
                                 {"my_column1": "like(\"%value%\")"},
                                 {"my_column2": "like(\"%value%\")"}
                            ]
                     }
    

    要使用'and'语句,只需将“or_”替换为“and_”

    【讨论】:

      猜你喜欢
      • 2015-10-16
      • 2021-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-06
      相关资源
      最近更新 更多