Subqueries

Execute subqueries using clauses, selected items, and attribute test values.

You can execute subqueries using the - (SUPQuery*)from:(SUPString)entity:(SUPString)alias method from SUPQuery. For example, the Objective-C code shown below is equivalent to this SQL query:
SELECT a.id FROM (SELECT b.id FROM AllType b) AS a WHERE a.id = 1
Use this Objective-C code:
SUPQuery *query1 = [SUPQuery getInstance];
[query1 select:@"b.id"];
[query1 from:@"AllType":@"b"];
SUPQuery *query2 = [SUPQuery getInstance];
[query2 select:@"a.id"];
[query2 fromQuery:query1:@"a"];
SUPAttributeTest *ts = [SUPAttributeTest getInstance];
ts.attribute = @"a.id";
[ts setTestValue:@"1"];
[query2 where:ts];
SUPQueryResultSet *qs = [SUP101DB executeQuery:query2];
You can use a subquery as the selected item of a query. Use the SelectItem to set selected items directly. For example, the Objective-C code shown below is equivalent to this SQL query:
SELECT (SELECT count(1) FROM AllType c WHERE c.id >= d.id) AS cn, id
FROM AllType d
Use this Objective-C code:
SUPQuery *selQuery = [SUPQuery getInstance];
[selQuery select:@"count(1)"];
[selQuery from:@"AllType":@"c"];
SUPAttributeTest *ttt = [SUPAttributeTest getInstance];
ttt.attribute = @"c.id";
ttt.operator = SUPAttributeTest_GREATER_EQUAL;
SUPColumn *cl = [SUPColumn getInstance];
cl.alias = @"d";
cl.attribute = @"id";
ttt.testValue = cl;
[selQuery where:ttt];
    
SUPObjectList *selectItems = [SUPObjectList getInstance];
SUPSelectItem *item = [SUPSelectItem getInstance];
item.query = selQuery;
item.asAlias = @"cn";
[selectItems add:item];
SUPQuery *subQuery2 = [SUPQuery getInstance];
subQuery2.selectItems = selectItems;
[subQuery2 from:@"AllType" :@"d"];
SUPQueryResultSet *qs = [SUP101DB executeQuery:subQuery2];