I didn't make note of from where in the forum I created the following, but here are two queries that can be created to address particular distribution lists for use in newsletters or other emails:
Code:
INSERT INTO query_builder (`query_category`, `query_name`, `query_description`, `query_string`, `query_keys_list`) VALUES ('email,newsletters', 'All customers who have NOT purchased product #196', 'All customers who have NEVER purchased product #196, ignoring newsletter-subscription status.', 'SELECT c.customers_email_address, c.customers_lastname, c.customers_firstname from TABLE_CUSTOMERS c WHERE c.customers_id NOT IN (SELECT c.customers_id FROM TABLE_CUSTOMERS c, TABLE_ORDERS o, TABLE_ORDERS_PRODUCTS op WHERE c.customers_id=o.customers_id AND o.orders_id=op.orders_id AND op.products_id=196) GROUP BY c.customers_email_address ORDER BY c.customers_lastname, c.customers_firstname ASC', '');
The above query (starting with SELECT in the middle of the INSERT INTO statement) returns the email address of all people that did not order the product number identified (196) in this case.
Code:
INSERT INTO query_builder (`query_category`, `query_name`, `query_description`, `query_string`, `query_keys_list`) VALUES ('email,newsletters', 'All customers who have purchased product #103', 'All customers who have ever purchased product #103, ignoring newsletter-subscription status.', 'select c.customers_email_address, c.customers_lastname, c.customers_firstname from TABLE_CUSTOMERS c, TABLE_ORDERS o, TABLE_ORDERS_PRODUCTS op WHERE c.customers_id = o.customers_id AND o.customers_id = c.customers_id AND o.orders_id = op.orders_id AND op.products_id = 103 GROUP BY c.customers_email_address ORDER by c.customers_lastname, c.customers_firstname ASC', '');
The above query can be run to add an email distribution list to select a list of people that have ordered from the website by product number. This was first used to add Item #37 by changing all cases of 103 to 37. Additional queries will be added to these notes as they become known.
I do see that in this second query there are back quotes used in the first set of parentheses. These may need to be changed to single quotes...
I haven't used the code in a long while but had captured it from the forum around July of 2013... Hopefully there is enough information provided to create the distribution list desired... Remember to honor your customers requests of notification/not being notified...