Home >Backend Development >Golang >Why Does My Go PostgreSQL LIKE Query Result in a Syntax Error?

Why Does My Go PostgreSQL LIKE Query Result in a Syntax Error?

Susan Sarandon
Susan SarandonOriginal
2024-12-09 05:41:10986browse

Why Does My Go PostgreSQL LIKE Query Result in a Syntax Error?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn