-
-
Notifications
You must be signed in to change notification settings - Fork 415
how to fetch data of latest foreign key in a multi-data foreign key table #1956
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 3 comments 5 replies
-
Does the table include a timestamp column that you could sort by to retrieve the last inserted record by workflow_id?
Beta Was this translation helpful? Give feedback.
All reactions
-
there is one column with timestamp, but i need all entries relating to last workflow_id and not just the latest one.
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
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
Beta Was this translation helpful? Give feedback.
All reactions
-
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
Beta Was this translation helpful? Give feedback.
All reactions
-
I believe you could do something similar to
SELECT * FROM table WHERE workflow_id = (SELECT MAX(workflow_id) FROM table)
Beta Was this translation helpful? Give feedback.
All reactions
-
yes, i am trying not to use native query...is there any way some method is made for this?
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.