使用LIKE?对不返回查询的Native SQLite进行React,并根据文本数组查询列?

xsuvu9jc  于 2023-03-19  发布在  SQLite
关注(0)|答案(1)|浏览(158)

执行此查询:
从菜单项中选择 *

return new Promise((resolve, reject) => {
 db.transaction((tx) => {
  tx.executeSql(
    "select * from menuitems",
    [`%${query}$%`],
    (_, { rows }) => {
      useDevLogData(rows._array, "filterByQueryAndCategories rows._array");
    }
  );
}, reject);
});

我有这组结果:

[
  {
    "id": 1,
    "uuid": "1.0",
    "title": "Spinach Artichoke Dip",
    "price": "10",
    "category": "Appetizers"
  },
  {
    "id": 2,
    "uuid": "2.0",
    "title": "Hummus",
    "price": "10",
    "category": "Appetizers"
  },
  ...
]

但是当我这样做的时候我没有结果:返回空[]

return new Promise((resolve, reject) => {
    db.transaction((tx) => {
      tx.executeSql(
        "select * from menuitems WHERE title LIKE ?",
        [`%${query}$%`],
        (_, { rows }) => {
          useDevLogData(rows._array, "filterByQueryAndCategories rows._array");
        }
      );
    }, reject);
  });

此外,我需要合并这个搜索文本,例如%菠菜%查询过滤类别的一个类别数组,例如.[“开胃菜”,“沙拉”],我如何查询过滤类别列基于字符串数组?并基于此,我需要查询字段标题?我需要这样做:

"select * from menuitems where title LIKE %Spinach% AND category LIKE ["Appetizers", "Salads"]"
krcsximq

krcsximq1#

对不起有错字,解决它这样,我有3最大类别:

activeCategories = [
  "Appetizers"
]

 return new Promise((resolve, reject) => {
    db.transaction((tx) => {
      tx.executeSql(
        "select * from menuitems WHERE title LIKE ? AND category = ? OR category = ? OR category = ?",
        [`%${query}%`, ...activeCategories],
        (_, { rows }) => {
          useDevLogData(rows._array, "filterByQueryAndCategories rows._array");
        }
      );
    }, reject);
  });

可能会帮助像我这样的SQL新手,这似乎对我现在很有效

相关问题