Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tasdemirbahadir/e96a494075579f0631f31ead0a9e7bb7 to your computer and use it in GitHub Desktop.
Save tasdemirbahadir/e96a494075579f0631f31ead0a9e7bb7 to your computer and use it in GitHub Desktop.
Check if records have any record inside a one-to-many relation table or not
SELECT
t.field1,
t.field2,
contains.id is not null contains_record
FROM
table t
LEFT JOIN
(select tom.* from table_one_to_many_relation tom where tom.id = 1) contains on contains.table_id = t.id
@tasdemirbahadir
Copy link
Author

This query is a design pattern to fetch records with the information if they contain an item from a one-to-many or many-to-many relation table

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment