Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

how to fetch data of latest foreign key in a multi-data foreign key table #1956

Unanswered
kartik-579 asked this question in Q&A
Discussion options

I have a table which have a foreign key 'workflow_id'. One 'workflow_id' can correspond to multiple entries in the table. I want to fetch all the entries of the latest 'workflow_id.

You must be logged in to vote

Replies: 3 comments 5 replies

Comment options

Does the table include a timestamp column that you could sort by to retrieve the last inserted record by workflow_id?

You must be logged in to vote
0 replies
Comment options

there is one column with timestamp, but i need all entries relating to last workflow_id and not just the latest one.

You must be logged in to vote
3 replies
Comment options

Is the workflow ID incremental, like a numeric sequence? If the workflow ID itself is not something you can sort by and get a meaningful order out of which one was the latest you might be hard pressed to get the data you need.

Comment options

the workflow_id is incremental(integer sequence) but I am confused on how to get the entries for the latest workflow_id, since the number of entries are not known

Comment options

if there were one entry for one workflow_id, i could have easily ordered the results and limit by 1 to get the desired results but i don't know how to achieve the same thing when there are multiple entries for one workflow_id

Comment options

I believe you could do something similar to
SELECT * FROM table WHERE workflow_id = (SELECT MAX(workflow_id) FROM table)

You must be logged in to vote
2 replies
Comment options

yes, i am trying not to use native query...is there any way some method is made for this?

Comment options

If you verify in psql or some other tool that that gives you the right data from the DB. I can help you turn that into a go-pg query.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /