I'm new to complex SQL queries and I've been asked to create a reporting solution to an existing store. I started with 3 tables (myOrders, myOrderedItems and myProducts) to keep it simple. The code below works great for what I wanted to do:
The trouble started when I tried to add the archive tables (myOrdersArchived and myOrderedItemsArchived) into the mix. The table myOrdersArchived has the same exact structure as myOrders. The table myOrderedItemsArchived has the same structure as myOrderedItems. It seems that I need to UNION the Orders tables as well as the OrderedItems tables. I've tried UNIONS and JOINS and Subqueries without any luck. I usually get one of the following errors:
The specified field 'OrderNumber' could refer to more than one table listed in the FROM clause of your SQL statement.
The specified field 'DateOfOrder' could refer to more than one table listed in the FROM clause of your SQL statement.
Syntax error in FROM clause.
So, any help or a pointer in the right direction would be greatly appreciated.
Code:
<cfquery name="qryMakeReport" datasource="#dsn#">
SELECT
myOrders.OrderNumber, myOrders.DateOfOrder, myOrderedItems.OrderNumber, myOrderedItems.CrtItemID, myOrderedItems.CrtQuantity, myProducts.ItemID, myProducts.ProductID, myProducts.ProductName, myProducts.InStock, myProducts.Category
FROM
myOrders, myOrderedItems, myProducts
WHERE
myOrders.OrderNumber = myOrderedItems.OrderNumber AND
myOrderedItems.CrtItemID = myProducts.ItemID AND
DateOfOrder BETWEEN #createODBCDate(startDate)# AND #createODBCDate(endDate)#
ORDER BY myOrders.DateOfOrder ASC
</cfquery>
The trouble started when I tried to add the archive tables (myOrdersArchived and myOrderedItemsArchived) into the mix. The table myOrdersArchived has the same exact structure as myOrders. The table myOrderedItemsArchived has the same structure as myOrderedItems. It seems that I need to UNION the Orders tables as well as the OrderedItems tables. I've tried UNIONS and JOINS and Subqueries without any luck. I usually get one of the following errors:
The specified field 'OrderNumber' could refer to more than one table listed in the FROM clause of your SQL statement.
The specified field 'DateOfOrder' could refer to more than one table listed in the FROM clause of your SQL statement.
Syntax error in FROM clause.
So, any help or a pointer in the right direction would be greatly appreciated.