Repo.get!() a specific column value from a result row

I’m getting my all row data using this command: Repo.all
Its returning me whole row by specific search.

ABC_TABLE
    |> where(another_table_id: ^another_table.id)
    |> Repo.all

Is it possible if I don’t take whole row just a specifc col value from resulted row?
Like this:

ABC_TABLE
    |> where(another_table_id: ^another_table.id)
    |> Repo.get!(name_only)

Seeking solution!

You might look at select.

I can’t find the way to adjust the SELECT PARAM inside my function. So, is there any function of Repo.get!(get_name_only)?

Can you should what you have tried?

This is my overall function:

 def subscriber_wallet_for_stream(%Stream{} = stream) do
    # Subscriber
    # |> where(stream_id: ^stream.id)
    # |> Repo.get!(:wallet_id)

    SELECT wallet_id FROM Subscriber
      WHERE stream_id = ^stream.id
  end

Just need any possible solution where I can call a single value of column of resulted search row using REPO.GET

Ecto mirrors SQL quite closely so if you examine the difference between your SQL statement and your Ecto expression you can see you are missing a |> select(....) expression in your pipeline. But equally, your commented code isn’t a valid Ecto expression so I suggest you revisit the query documentation to see how to construct a query.

2 Likes

You might have a look at the link provided, there are some examples…

Thank you so much @kokolegorille @kip for helping.
I go through the doc and finally developed the query:

 Repo.all(from s in Subscriber, where: s.stream_id == ^stream.id, select: s.wallet_id)

Well done. You could also do Repo.one if you just want a single result, not a list. However if the query returns more than one result then an exception will be raised.

2 Likes