如何使用 knex 查询 PostGresQl 检查逗号分隔字段中的单个值
How to check single value in coma seprated field using knex query PostGresSQl
我有 table 个城市,其中邮政编码以逗号分隔,如下例
CityId CountryId StateId CityName Zipcodes
1 1 1 NewYork 16066,15825,35114
2 1 1 Cranbarry 11478,88544,97544
像这样我有一堆美国城市和邮政编码的数据
但是当我尝试使用 Knex Query
搜索它时,我如何编写我们用于在 MySql
中搜索的相同查询
Select * from cities Where FIND_IN_SET('16066',Zipcodes)
Note: FIND_IN_SET is unsupported in Knex and PostGresSql
在 knex 查询中。或者是否有任何其他方法可以使用 Knex 查询
在此逗号分隔值中按邮政编码查找城市
因为我工作的平台是 Node.js 和 PostGresSQl
你可以通过将字符串转换成数组来做到这一点,这里就是你的答案
SELECT id,name, zipcode FROM cities,
unnest(string_to_array(zipcode, ',')) AS zipcodes
where
zipcodes = ANY ( string_to_array('16066', ',') ) group by id
我有 table 个城市,其中邮政编码以逗号分隔,如下例
CityId CountryId StateId CityName Zipcodes
1 1 1 NewYork 16066,15825,35114
2 1 1 Cranbarry 11478,88544,97544
像这样我有一堆美国城市和邮政编码的数据
但是当我尝试使用 Knex Query
搜索它时,我如何编写我们用于在 MySql
Select * from cities Where FIND_IN_SET('16066',Zipcodes)
Note: FIND_IN_SET is unsupported in Knex and PostGresSql
在 knex 查询中。或者是否有任何其他方法可以使用 Knex 查询
在此逗号分隔值中按邮政编码查找城市因为我工作的平台是 Node.js 和 PostGresSQl
你可以通过将字符串转换成数组来做到这一点,这里就是你的答案
SELECT id,name, zipcode FROM cities,
unnest(string_to_array(zipcode, ',')) AS zipcodes
where
zipcodes = ANY ( string_to_array('16066', ',') ) group by id