#Help need in writing a SQL Query
15 messages · Page 1 of 1 (latest)
you don't need a join for this query, you should only join a table if you need data from both tables, instead you shoud use subquery SQL SELECT DISTINCT * FROM ideas WHERE id IN (SELECT idea_id FROM idea_tags WHERE tag_id IN (4,5));
don't use a subquery for this btw, it is probably slower than doing a simple join
@visual swan I did try to do this like this:
SELECT * FROM ideas, idea_tags WHERE ideas.id = idea_tags.idea_id AND tag_id in (4, 5);
But then it gives me duplicated rows 🥲
This query returns me two rows when in fact it should have returned only one
According to this join table
that is a cross join
select * from ideas left join idea_tags on idea_tags.idea_id = idea.id where tag.id in (4,5)
Can you tell me how to do this using gorm.io ?
Not familiar with gorm. You have to setup your model objects and relations correctly
how is your model looks like?