I want to execute a subquery like:
SELECT id, col1, col2 FROM table1 WHERE col1='val1' and col2 NOT IN (
SELECT ID FROM table2 WHERE col1='val1' and col3 = 'val3')
How can I use GORM to execute it?
Write a standard query and then call the .SubQuery()
method:
sub := db.Table("table2").Select("ID").Where("col1 = ?", 'val1').SubQuery()
The you can place it as a parameter in the .Where()
method
err := db.Table("table1").Where("col2 NOT IN ?", sub).Find(&table1Type).Error
//handle the error