Thanks for taking a look @ this for me. I think this is what you asked for in the way of table structure. Here's the fields in my products table:
CREATE TABLE `products` (
`products_id` int(11) NOT NULL auto_increment,
`products_company_id` int(11) NOT NULL default '0',
`products_name` varchar(90) NOT NULL default '',
`products_status` tinyint(4) NOT NULL default '0',
`products_type_of_billing` tinyint(1) NOT NULL default '0',
`products_invoice_amount` decimal(10,2) NOT NULL default '0.00',
PRIMARY KEY (`products_id`)
)
And then my monthly_invoices table looks like this:
CREATE TABLE `monthly_invoices` (
`invoice_id` int(11) NOT NULL auto_increment,
`invoice_number` varchar(20) NOT NULL default '',
`invoice_company_id` int(11) NOT NULL default '0',
`invoice_date` varchar(14) NOT NULL default '',
`invoice_amount` decimal(10,2) NOT NULL default '0.00',
`invoice_freq_of_billing` tinyint(1) NOT NULL default '0',
`invoice_paid` tinyint(1) NOT NULL default '0',
PRIMARY KEY (`invoice_id`)
)
So when this cron job runs every month, I need to look for items in the Products table that have a invoice_amount ("WHERE products_paid_up_through <= 1146855189 AND products_invoice_amount > '0.00'"), and then pull in the LAST invoice that was sent for that company (LEFT JOIN monthly_invoices ON monthly_invoices.invoice_company_id=products.products_company_id). The monthly invoices table is a log, so it will typically have multiple entries for each company and product. I want to pull just the most recent invoice_date into this query.