Hi,
I am trying to rewrite an SQL query using query builder (in order to take advantage of design time checking, to make the code database agnostic and because I think that it makes my code far easier to maintain) but I cannot get my head around how to structure the query. I am trying to return a dataset that consists of three fields from three related tables. Using the example_data database an equivalent to my query would be:
query = "SELECT orders.orderid, \
customers.companyname, \
employees.lastname \
FROM orders \
JOIN customers \
ON customers.customerid = orders.customerid \
JOIN employees \
ON employees.employeeid = orders.orderid "
result = databaseManager.getDataSetByQuery("example_data",query,null,100);
This returns a dataset that has three fields (one from each table) showing the order ID, customer name and employee surname.
I have tried to replicate this using the query builder syntax but cannot find a way to add just the columns I want to the result set. What I have so far is:
var args = null;
/** @type {QBSelect<db:/example_data/orders>} */
var query = databaseManager.createSelect('db:/example_data/orders');
/** @type {JSDataSet} */
var result = null;
query.result.add(query.columns.orderid);
result = databaseManager.getDataSetByQuery(query,100);
This gives a dataset that contains the orderid but nothing more. I ave researched the wiki and the forums but can find no examples of adding a column from a related table to the result set but lots that show how to filter the results using related table columns in the ‘where’ clause. I have tried to replicate this type of syntax in my query but Servoy reports everything I try as an error in the designer (I don’t even get to the stage of running it!). The examples I have tried to follow are from the QBCondition page on the wiki:
https://wiki.servoy.com/display/public/DOCS/QBWhereCondition
as well as the release notes for 6.1 which describes the query builder :
https://wiki.servoy.com/display/SERV61/Query+builder
The first link would result in something similar to:
query.result.add(query.joins.orders_to_employees.joins.orders_to_employees.columns.lastname
but the developer doesn’t like this syntax and errors on the second ‘joins’. I have also tried following the syntax in the second link:
query.result.add(query.joins.orders_to_employees.columns.lastname
but again the developer doesn’t like this and errors on the ‘columns’ element.
Can anyone point me in the right direction please?
Thanks
Steve