Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7872083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:06:10+00:00 2026-06-03T02:06:10+00:00

I am creating a script that searches a database for abandoned carts and emails

  • 0

I am creating a script that searches a database for abandoned carts and emails the contents to the customer (product name, image, quantity etc). The data is spread across a number of tables and as a far as getting the information and emailing it – so far so good. However it is sending one email per product that the customer has put in their cart. So if they have 5 items in their cart, it sends 5 emails. I know this is because of my join and while statement but my php/sql knowledge isn’t very good.

So my question is which join or union statement should I be using so that an email is only sent once to one customer containing all of their products.

The tutorials I’ve read about Joins always have 1 id or key that is common between all tables, however with the database structure of this store one table relates to another based off 1 key, and then that table uses a different key to relate to the other reliant table.

Database Structure Looks like This.

ORDERS:
|   orderid    |   ordstatus   |   ordqty   |  ordcustid   |
|--------------|---------------|------------|--------------|
|    12637     |       0       |      1     |        0     |
|    12636     |      11       |      1     |     3531     |
|    12635     |      11       |      2     |     4192     |

ORDERPRODUCTS:
|  ordprodname  |  ordprodqty  |  orderorderid  |  orderprodid   |
|---------------|--------------|----------------|----------------|
|   product1    |      1       |      12637     |      1206      |
|   product2    |      1       |      12636     |       193      |
|   product3    |      1       |      12635     |      1712      |
|   product4    |      1       |      12635     |      1576      |

CUSTOMERS:
|  customerid  |  customeremail    |  firstname  |
|--------------|-------------------|-------------|
|     3531     | cust1@gmail.com   |  Customer1  |
|     4192     | cust2@gmail.com   |  Customer2  |

PRODUCTIMAGE:
|  imageid  |  imageprodid  |   imagethumb     |
|-----------|---------------|------------------|
| Product1  |      1206     | file/path/1.jpg  |
| Product2  |       193     | file/path/2.jpg  |
| Product3  |      1712     | file/path/3.jpg  |
| Product4  |      1576     | file/path/4.jpg  |

So far my script looks like this:

mysql_select_db("cartmail", $con);

$result = mysql_query("SELECT * 
            FROM    orders
                ,   orderproducts
                ,   productimage
                ,   customers
            WHERE   orders.orderid              = orderproducts.orderorderid
            AND     orderproducts.orderprodid   = productimage.imageprodid
            AND     orders.ordcustid            = customers.customerid
            AND     ordstatus = '11'
            LIMIT 0 , 30");

while($row = mysql_fetch_array($result))
{
    <My PHP mail script...>
}

mysql_close($con);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T02:06:12+00:00Added an answer on June 3, 2026 at 2:06 am

    Following query uses GROUP_CONCAT in MySQL to concatenate the product details in the order and provides single column value for each customer. The result set contains unique customer id, customer name, customer email along with with the concatenated list of their order details. Hopefully that gives you an idea to use the concatenated order information in the email body that will be sent to customers.

    I don’t know PHP but the query should give you an idea on how to fetch the results.

    Click here to view the demo in SQL fiddle.

    Script:

    CREATE TABLE orders
    (
        orderid INT NOT NULL
      , orderstatus INT NOT NULL
      , ordqty INT NOT NULL
      , ordcustid INT NOT NULL
    );
    
    CREATE TABLE orderproducts
    (
        ordprodname     VARCHAR(30) NOT NULL
      , ordprodqty      INT         NOT NULL
      , orderorderid    INT         NOT NULL
      , orderprodid     INT         NOT NULL
    );
    
    CREATE TABLE customers
    (
        customerid      INT         NOT NULL
      , customeremail   VARCHAR(30) NOT NULL
      , firstname       VARCHAR(30) NOT NULL
    );
    
    CREATE TABLE productimage
    (
        imageid         VARCHAR(30)     NOT NULL
      , imageprodid     INT             NOT NULL
      , imagethumb      VARCHAR(30)     NOT NULL
    );
    
    INSERT INTO orders (orderid, orderstatus, ordqty, ordcustid) VALUES
       (12637, 0,   1, 0),
       (12636, 11,  1, 3531),
       (12635, 11,  2, 4192);
    
    INSERT INTO orderproducts (ordprodname, ordprodqty, 
                orderorderid, orderprodid) VALUES
       ('product1', 1, 12637, 1206),
       ('product2', 1, 12636, 193),
       ('product3', 1, 12635, 1712),
       ('product4', 1, 12635, 1576);
    
    INSERT INTO customers (customerid, customeremail, firstname) VALUES
       (3531, 'cust1@gmail.com', 'Customer1'),
       (4192, 'cust2@gmail.com', 'Customer2'),
       (1111, 'cust3@gmail.com', 'Customer3');
    
    INSERT INTO productimage (imageid, imageprodid, imagethumb) VALUES
       ('Product1', 1206,   'file/path/1.jpg'),
       ('Product2', 193,    'file/path/2.jpg'),
       ('Product3', 1712,   'file/path/3.jpg'),
       ('Product4', 1576,   'file/path/4.jpg');
    
    SELECT          c.customerid
                ,   c.firstname
                ,   c.customeremail,
                    group_concat('Order Id: ', orderid, 
                        ' | Product name: ', ordprodname, 
                        ' | Quantity: ', ordprodqty, '<br>') AS ordered_items,
    FROM            customers c
    LEFT OUTER JOIN orders o
    ON              o.ordcustid = c.customerid
    LEFT OUTER JOIN orderproducts op
    ON              op.orderorderid = o.orderid
    LEFT OUTER JOIN productimage pi
    ON              pi.imageprodid = op.orderprodid
    GROUP BY        c.customerid
    HAVING          COUNT(DISTINCT o.ordcustid) > 0;
    

    Output:

    CUSTOMERID FIRSTNAME   CUSTOMEREMAIL     ORDERED_ITEMS
    ---------- ----------- ----------------  ------------------------------------
    3531       Customer1   cust1@gmail.com   Order Id: 12636 | Product name: product2 | Quantity: 1<br>
    4192       Customer2   cust2@gmail.com   Order Id: 12635 | Product name: product4 | Quantity: 1<br>,
                                             Order Id: 12635 | Product name: product3 | Quantity: 1<br> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a php backup script that will dump everything from a database
I'm creating a PHP script that fetches IDs from a database. The results are
I am creating a notifications script that check a database for changes and returns
I am creating a script that allows the user to choose their own timezone...
I am creating a little script that allows me to slide my content sections
I am creating a script in JS that will be called from external sites,
I am creating a bash script for generating certificates. The openssl command that creates
I am creating a program that communicates with a PHP script on a web
I'm creating a python script that runs rsync using subprocess and then gets the
I'm creating a PowerShell script that I'm going to execute using Start-Job . The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.