If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Insert

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-11-04, 08:26
Jen76 Jen76 is offline
Registered User
 
Join Date: Jan 2004
Location: California
Posts: 10
Insert

Here's my queries:

INSERT INTO Bills (Cust_ID) SELECT Customers.Cust_ID FROM Customers WHERE Customers.Bill_Cycle_ID = "1"

and

INSERT INTO Item_Billing_Link (Product_ID) SELECT Products.Product_ID FROM Products WHERE Products.Recurring = "yes" AND Products.Cust_ID = "?"


My question...how can I do this without having to manually go thru and enter each customer in the second query that was selected in the first query?
Reply With Quote
  #2 (permalink)  
Old 02-11-04, 09:46
timclaason timclaason is offline
Registered User
 
Join Date: Nov 2003
Posts: 9
Re: Insert

Quote:
Originally posted by Jen76
Here's my queries:

INSERT INTO Bills (Cust_ID) SELECT Customers.Cust_ID FROM Customers WHERE Customers.Bill_Cycle_ID = "1"

and

INSERT INTO Item_Billing_Link (Product_ID) SELECT Products.Product_ID FROM Products WHERE Products.Recurring = "yes" AND Products.Cust_ID = "?"


My question...how can I do this without having to manually go thru and enter each customer in the second query that was selected in the first query?
I suppose you could load the recordset into a separate array, and then use a loop to stick them back into the other table.
Reply With Quote
  #3 (permalink)  
Old 02-11-04, 12:27
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
in general, there is almost always an sql solution which makes looping unnecessary, and this is true in this case as well
Code:
insert 
  into Item_Billing_Link 
     ( Product_ID
     ) 
select Products.Product_ID 
  from Customers 
inner
  join Products 
    on Customers.Cust_ID = Products.Cust_ID
 where Customers.Bill_Cycle_ID = '1' 
   and Products.Recurring = 'yes'
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 02-12-04, 00:12
Jen76 Jen76 is offline
Registered User
 
Join Date: Jan 2004
Location: California
Posts: 10
Wonderful! Thanks Guys!!!
Reply With Quote
  #5 (permalink)  
Old 02-12-04, 00:19
Jen76 Jen76 is offline
Registered User
 
Join Date: Jan 2004
Location: California
Posts: 10
Also...in the first query when the cust_id is entered into the bills table the bill_id auto increments. I know I can use "last_insert_id" but it only grabs the first id not all of the id's if there's more than one. Is there a way to grab all id's just created in order to again insert into the item_billing_link table along with the product_id.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On