LIKE with % on column names

2019-03-11 18:36发布

Here is my query that results in a syntax error:

SELECT * 
FROM account_invoice,sale_order
WHERE sale_order.name LIKE %account_invoice.origin%

The account_invoice.origin field contains the text of sale_order.name, plus other text as well, so I need to match sale_order.name string anywhere in the account_invoice.origin string.

I'm using PostgreSQL 8.4.

1条回答
地球回转人心会变
2楼-- · 2019-03-11 19:18

Try this

SELECT * 
FROM account_invoice,sale_order
WHERE sale_order.name LIKE '%'  || account_invoice.origin || '%'

% needs single quote because the pattern is a string.

|| is the operator for concatenation.

查看更多
登录 后发表回答