Jump to content

nest query problem please help


geroido

Recommended Posts

Hi all

I'm trying to query the results of a select statement. The inner select is working perfectly and returning the result set. However, I want to query this result set and only get  distinct records (with a distinct 'Ordernum') as there are several records returned with the same Ordernum value and I expect this. For example, there are several rows retrieved with an Ordernum of 621 but I only want one of these rows displayed. Can you help. Is it a syntax problem?

 

$query = "select DISTINCT(orderdetails.Ordernum), orderdetails.MenuItemID, orderdetails.CustFName, 
orderdetails.CustSName, orderdetails.StreetAddr, orderdetails.Town, orderdetails.County, 
orderdetails.orddate from 
(select orderdetails.Ordernum, orderdetails.MenuItemID, orderdetails.CustFName, 
orderdetails.CustSName, orderdetails.StreetAddr, orderdetails.Town,  orderdetails.County, 
orderdetails.orddate FROM orderdetails join menuitemdetails on orderdetails.MenuItemID=menuitemdetails.MenuItemID 
where menuitemdetails.MenuItemID = orderdetails.MenuItemID ) order by orderdetails.CustSName asc"; 

Link to comment
Share on other sites

Hi fenway

Thanks for the help. I've tried GROUP BY - it's still not working. Any other thoughts?

 

$query = "select DISTINCT(Ordernum), MenuItemID, CustFName, 
CustSName, StreetAddr, Town, County, 
orddate from orderdetails where orderdetails.Ordernum =
(select orderdetails.Ordernum, orderdetails.MenuItemID, orderdetails.CustFName, 
orderdetails.CustSName, orderdetails.StreetAddr, orderdetails.Town,  orderdetails.County, 
orderdetails.orddate FROM orderdetails join menuitemdetails on orderdetails.MenuItemID=menuitemdetails.MenuItemID 
where menuitemdetails.MenuItemID = orderdetails.MenuItemID ) group by orderdetails.Ordernum "; 

Link to comment
Share on other sites

Hi

Just to explain what I'm trying to do:

 

This is driving me crazy. Let me explain what I want a little better. I have two tables

Table 1 (orderdetails)

Ordernum, MenuItemID, CustFName, CustSName, StreetAddr, Town, County

 

Table 2 (menuitemdetails)

ClientID, MenuItemID, MenuItemName etc.

 

So heres the problem:

 

I have a client who logs in and I save there clientid in a session variable called $S_SESSION['userid'];. They can click a button and view all of the customers who have placed orders with them. So I want the query to scan through orderdetails table selecting all records that have a matching MenuItemID in menuitemdetails which has a ClienID = $S_SESSION['userid'];. This means that only orders for this particular client will be retrieved. Can you please help? I.ve been trying this:

 

$query = "select DISTINCT(orderdetails.Ordernum), orderdetails.MenuItemID, orderdetails.CustFName,
orderdetails.CustSName, orderdetails.StreetAddr, orderdetails.Town,  orderdetails.County,
orderdetails.orddate, menuitemdetails.ClientID FROM orderdetails  join menuitemdetails on orderdetails.MenuItemID=menuitemdetails.MenuItemID
where ClientID = '".$S_SESSION['userid']."' group by CustFName ";

Link to comment
Share on other sites

Hi fenway

Thanks for the help. I've tried GROUP BY - it's still not working. Any other thoughts?

 

$query = "select DISTINCT(Ordernum), MenuItemID, CustFName, 
CustSName, StreetAddr, Town, County, 
orddate from orderdetails where orderdetails.Ordernum =
(select orderdetails.Ordernum, orderdetails.MenuItemID, orderdetails.CustFName, 
orderdetails.CustSName, orderdetails.StreetAddr, orderdetails.Town,  orderdetails.County, 
orderdetails.orddate FROM orderdetails join menuitemdetails on orderdetails.MenuItemID=menuitemdetails.MenuItemID 
where menuitemdetails.MenuItemID = orderdetails.MenuItemID ) group by orderdetails.Ordernum "; 

What does this produce?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.