-1

I am using sqlboiler and golang

using this code I fetch all details from the database.

userDemo, erro := dbmodels.UserDemographics().All(ctx, m.db)

UserDemographic table contain 2 columns, userId and count

how can I fetch the data the userId=1 from userDemo. (we can pass userId=1 in function All, that is not I want)

after fetching the data from db(here eg userDemo), how can I get each row of data diffeneltly

Mia Mia
  • 143
  • 12
  • What exactly do you want when you say `(we can pass userId=1 in function All, that is not I want)`? If not this, you can pass `dbmodels.UserDemographics(Where("id = ?", 1)).All(ctx, m.db)`. Also, id (1 here) can be a variable. Does this not work for you? – advay rajhansa Jun 26 '21 at 10:09
  • @advayrajhansa yes that works. i have a set of data like 1,2,3,4 .. after fetching the data from DB i want to check in the result set there exist a data that ID =2 , if exist i want to get a data from the result... I am looking for avoing a for loop – Mia Mia Jun 27 '21 at 20:13

1 Answers1

0

The requirement here is for a search. There are two places this can be achieved, either you try to query specific userid or with all results in hand, you can try to run it through a searching algorithm. Given that you are using go, there is no idiomatic way to achieve this in a one-liner.

The best possible way is to write a for loop for userDemo. If you want, there is a library called funk which can help you with this search.

advay rajhansa
  • 1,129
  • 9
  • 17