You're trying to execute a MySQL query in Go that sets variables before running the query. However, when you run the query through Go, you're encountering a syntax error at the SELECT statement.
The solution to this problem involves two steps:
Configure DSN:
Convert Collation:
DSN Configuration:
The multiStatements setting allows multiple statements to be executed in a single query. The interpolateParams setting enables parameter interpolation, which allows you to use ? placeholders for query parameters.
Collation Conversion:
MySQL has multiple collations, which determine how characters are sorted and compared. If the collation of the database and tables doesn't match, you can encounter collation errors when performing comparisons. Converting to a consistent collation resolves this issue.
By making these changes, you'll be able to execute your query successfully in Go while setting variables beforehand.
The above is the detailed content of How to Execute MySQL Queries in Go with Set Variables. For more information, please follow other related articles on the PHP Chinese website!