2
votes

I am new to this language and having some problems understand queries.

for example:

There are 2 database tables: Warehouse, product. So each warehouse can have multiple products and products can be stored in different warehouses.

Query:
   for each warehouse,
       each product:
   display warehouse.name, product.prodcode.
end.

the display will like

warehousename   productcode
awarehouse       SKA-301

so for this result, are these columns display total independent result, eg. SKA-301 product may not in awarehouse. Or it will display the product in awarehouse? what if product and warehouse don't have related fields?

Please help me. Thank you.

2
Note - commas "," don't belong between fields in a display statement. - Tim Kuehn

2 Answers

4
votes

In the code that you have shown you will get each product for every iteration of warehouse.

To get the products that are specific to a particular warehouse you need to add WHERE criteria to the 2nd clause of the join. Assuming that you have a product.warehouseName field that would suit this purpose....

for each warehouse no-lock,
  each product no-lock where product.warehouseName = warehouse.name:

  display
    warehouse.name
    product.prodcode
  .

end.

(If there is no index on product.warehouseName this will be very inefficient.)

0
votes

Your query is compact version of this expanded version

for each warehouse:
   for each product:
      display warehouse.name, product.prodcode. 
   end.
end.

They both achieve the same thing. I would suggest since you are starting off to expand the query out and once you understand the relationships then go back to the compact each, each version