![]() Query ( "SELECT id, first_name FROM users LIMIT $1", 3 ) if err != nil You can use the following SQL code to create a table and insert a few records if you haven’t already. It also assumes that you have a valid connection to your database in your code (see earlier posts in this series if you need help with this any of this). This article assumes you have a single table named users in your Postgres database along with a few records to query. In this article we are going to cover the second use case - querying for a multiple records. ![]() ![]() In Go 1.8 support for multiple result sets was added, making this much simpler. To achieve the last you would need to pass data back and forth between queries (or construct different SQL queries). Up until recently, only the first two use cases were supported by Go’s database/sql package. One example of this might be if you wanted to create a temporary table of users with a certain attribute, and then query for many pieces of information about that user. This use case is a little rarer, but it generally pops up when you want to use some intermediary data between queries. You are interested in multiple result sets. ![]() Eg you might want all of the comments created by a specific user. You want to retrieve many records from the same table (or through joins).For example, you might want to look up a specific user. When you are querying for SQL records, I find that you generally fall into one of three use cases: Querying for multiple records with Go's sql package ![]()
0 Comments
Leave a Reply. |