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
Release: 2024-12-09 05:41:10
Original
974 people have browsed it

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
Copy after login

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;
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template