如何在代码中构建或条件化数据源?
How to build or condition on datasorce in the code?
我有两张桌子。让我们称它们为 Table1 和 Table2。我需要像这样在代码查询中创建。
Select * from Table1
exists join Table2 where
Table1.Key == Table2.Key &&
(Table1.A == '1' || Table2.C == '2')
因此要转移到我尝试过的代码:
Query q;
QueryBuildDataSource qbd;
QueryBuildDataSource ofQbds;
QueryBuildRange qbr;
str range;
Table1 test;
QueryRun qr;
q = new Query();
qbd = q.addDataSource(tableNum(Table1));
qbd .name('Table1');
ofQbds = qbd.addDataSource(tableNum(Table2));
ofQbds.name('Table2');
ofQbds.addLink(fieldNum(Table1, Key), fieldNum(Table2, Key));
ofQbds.joinMode(JoinMode::InnerJoin);
range = strFmt('%1.%2 == %3 OR %4.%5 == %6', qbd.name(), 'A', '1',
ofQbds.name(), 'C', '5');
info(range);
ofQbds.addRange(fieldName2id(tableNum(Table1 ), 'A')).value(range);
qr = new QueryRun(q);
while (qr.next())
{
test = qr.get(test.TableId);
info(test.A + ','+test.Key);
}
info('done');
但是 or 条件不能正常工作。
例如如果我有值
Table1
A,Key
1,1
2,2
3,3
Table2
C,Key
2,2
我希望表 1 有 2 个结果
1,1
2,2
但我只得到第一个
Message
Table1.A == 1 OR Table2.C == 2
2,2
done
我尝试了不同的加入模式和切换 ||和 OR 但我尝试过的都没有用。
我需要更改什么才能在代码中拥有或条件?
尝试更改此行:
range = strFmt('%1.%2 == %3 OR %4.%5 == %6', qbd.name(), 'A', '1', ofQbds.name(), 'C', '5');
到
range = strFmt('((%1.%2 == %3) || (%4.%5 == %6))', qbd.name(), 'A', '1', ofQbds.name(), 'C', '5');
不要使用 sql "or",而是使用 x++ "||"
编辑:有关详细信息,请参阅 http://www.axaptapedia.com/Expressions_in_query_ranges。
编辑 2:这段代码以我想要的方式执行。我建议使用最新的编译时函数,例如 tableNum()
和 fieldNum()
等
Query q;
QueryBuildDataSource qbd;
QueryBuildDataSource ofQbds;
QueryBuildRange qbr;
str range;
QueryRun qr;
CustTable custTable;
CustGroup custGroup;
q = new Query();
qbd = q.addDataSource(tableNum(CustTable));
ofQbds = qbd.addDataSource(tableNum(CustGroup));
ofQbds.addLink(fieldNum(CustTable, CustGroup), fieldNum(CustGroup, CustGroup));
ofQbds.joinMode(JoinMode::InnerJoin);
range = strFmt('((%1.%2 == %3) || (%4.%5 == %6))',
tableStr(CustTable),
fieldStr(CustTable, AccountNum),
'000001',
tableStr(CustGroup),
fieldStr(CustGroup, CustGroup),
'UAC');
info(range);
ofQbds.addRange(fieldNum(CustTable, AccountNum)).value(range);
qr = new QueryRun(q);
while (qr.next())
{
custTable = qr.get(tableNum(CustTable));
info(custTable.AccountNum + ',' +custTable.CustGroup);
}
我有两张桌子。让我们称它们为 Table1 和 Table2。我需要像这样在代码查询中创建。
Select * from Table1
exists join Table2 where
Table1.Key == Table2.Key &&
(Table1.A == '1' || Table2.C == '2')
因此要转移到我尝试过的代码:
Query q;
QueryBuildDataSource qbd;
QueryBuildDataSource ofQbds;
QueryBuildRange qbr;
str range;
Table1 test;
QueryRun qr;
q = new Query();
qbd = q.addDataSource(tableNum(Table1));
qbd .name('Table1');
ofQbds = qbd.addDataSource(tableNum(Table2));
ofQbds.name('Table2');
ofQbds.addLink(fieldNum(Table1, Key), fieldNum(Table2, Key));
ofQbds.joinMode(JoinMode::InnerJoin);
range = strFmt('%1.%2 == %3 OR %4.%5 == %6', qbd.name(), 'A', '1',
ofQbds.name(), 'C', '5');
info(range);
ofQbds.addRange(fieldName2id(tableNum(Table1 ), 'A')).value(range);
qr = new QueryRun(q);
while (qr.next())
{
test = qr.get(test.TableId);
info(test.A + ','+test.Key);
}
info('done');
但是 or 条件不能正常工作。 例如如果我有值
Table1
A,Key
1,1
2,2
3,3
Table2
C,Key
2,2
我希望表 1 有 2 个结果
1,1
2,2
但我只得到第一个
Message
Table1.A == 1 OR Table2.C == 2
2,2
done
我尝试了不同的加入模式和切换 ||和 OR 但我尝试过的都没有用。
我需要更改什么才能在代码中拥有或条件?
尝试更改此行:
range = strFmt('%1.%2 == %3 OR %4.%5 == %6', qbd.name(), 'A', '1', ofQbds.name(), 'C', '5');
到
range = strFmt('((%1.%2 == %3) || (%4.%5 == %6))', qbd.name(), 'A', '1', ofQbds.name(), 'C', '5');
不要使用 sql "or",而是使用 x++ "||"
编辑:有关详细信息,请参阅 http://www.axaptapedia.com/Expressions_in_query_ranges。
编辑 2:这段代码以我想要的方式执行。我建议使用最新的编译时函数,例如 tableNum()
和 fieldNum()
等
Query q;
QueryBuildDataSource qbd;
QueryBuildDataSource ofQbds;
QueryBuildRange qbr;
str range;
QueryRun qr;
CustTable custTable;
CustGroup custGroup;
q = new Query();
qbd = q.addDataSource(tableNum(CustTable));
ofQbds = qbd.addDataSource(tableNum(CustGroup));
ofQbds.addLink(fieldNum(CustTable, CustGroup), fieldNum(CustGroup, CustGroup));
ofQbds.joinMode(JoinMode::InnerJoin);
range = strFmt('((%1.%2 == %3) || (%4.%5 == %6))',
tableStr(CustTable),
fieldStr(CustTable, AccountNum),
'000001',
tableStr(CustGroup),
fieldStr(CustGroup, CustGroup),
'UAC');
info(range);
ofQbds.addRange(fieldNum(CustTable, AccountNum)).value(range);
qr = new QueryRun(q);
while (qr.next())
{
custTable = qr.get(tableNum(CustTable));
info(custTable.AccountNum + ',' +custTable.CustGroup);
}