我有一个表,其中的字段表示该行中的数据是否有效。该字段实际上是每个订单的代码编号的组合。此字段包含长度不确定的字符串。我需要一个查询,该查询将只提取该字段中所有字符都包含“ AT”的行。
SELECT *
FROM
(SELECT o.completed_at::TIMESTAMP::date AS "Date",
u.email AS "User Email",
o.number AS "Order Number",
string_agg(sp.name, ', ') AS "Product Name",
o.total AS "Total Price",
ssl.name AS "Store Name",
string_agg(spt.code, ', ') AS code_number
FROM spree_orders o
LEFT JOIN spree_users u ON o.user_id = u.id
LEFT JOIN spree_shipments ss ON ss.order_id = o.id
LEFT JOIN spree_stock_locations ssl ON ssl.id = ss.stock_location_id
LEFT JOIN spree_line_items sli ON sli.order_id = o.id
LEFT JOIN spree_variants sv ON sv.id = sli.variant_id
LEFT JOIN spree_products sp ON sp.id = sv.product_id
LEFT JOIN spree_product_types spt ON spt.id = sp.product_type_id
WHERE o.country_id = 2
AND o.state = 'complete'
AND ss.state = 'delivered'
GROUP BY 1,
2,
3,
5,
6
ORDER BY 1 DESC, 2)a
WHERE a.code_number LIKE '%[^AT]%'
当前输出如下:
FC573, MS355, AT256,BKR004, DA261, BEV178, FR133, DA304, FP433, FP097, PET025, AT203
AT101, AT256, AT5465
预期结果
AT101, AT256, AT5465
它应该只显示初始值为“ AT”的行
答案 0 :(得分:0)
根据我的理解,您想匹配一个仅包含数字,空格,逗号以及字母“ A”和“ T”的组合的值。您可以使用SIMILAR TO运算符以正则表达式的形式构建所需的内容,例如
WHERE a.code_number SIMILAR TO '[0-9, AT]+'
或者,如果您希望以AT开头,后跟数字并以逗号和空格分隔的显式序列,也可以将其匹配:
WHERE a.code_number SIMILAR TO '([AT[0-9]+])(, [AT[0-9]+])*'