Go PostgreSQL LIKE Query Syntax Error
When using the pq driver for PostgreSQL in Go, you may encounter a syntax error similar to "pq: syntax error at or near "%" when executing a LIKE query like this:
SELECT p.id, p.name, p.description, p.price, p.image, p.rate FROM products AS p WHERE LOWER(p.name) LIKE %% ORDER BY p.rate DESC
This error occurs because you need to quote the like pattern correctly in Go. The correct syntax would be:
SELECT p.id, p.name, p.description, p.price, p.image, p.rate FROM products AS p WHERE LOWER(p.name) LIKE '%' || || '%' ORDER BY p.rate DESC;
By enclosing the LIKE pattern in single quotes, you ensure that the % characters are treated as literals and not as wildcard characters.
The above is the detailed content of Why Does My Go PostgreSQL LIKE Query Result in a Syntax Error?. For more information, please follow other related articles on the PHP Chinese website!