Page 2 of 2 FirstFirst 12
Results 11 to 20 of 20
  1. #11
    Join Date
    Aug 2004
    Posts
    1,590
    Plugin Contributions
    1

    Default Re: SQL query help requested

    No, actually, as said above - you're using the same table so no need to do that.

    Without knowing the rest of your codings,

    try this one instead:

    PHP Code:
    SELECT customers_iddate_purchasedorder_total 
    FROM zen_orders
    WHERE customers_id 
    ".(int)$_SESSION['customer_id']." AND date_purchased <> "2006-07-01 00:00:00" AND date_purchased "2006-08-01 00:00:00"
    ORDER BY date_purchased DESC
    "; 
    That's all you'd need to do really . . .

    As for selecting your order_total field, I don't know why it has been selected if you have no intentions of using it under any conditions (unless it is being used below the block you have posted from your personal project file). If not, you can delete it from the SQL Select statement to free up a little bit more SQL server ressources.

  2. #12
    Join Date
    Aug 2004
    Posts
    1,590
    Plugin Contributions
    1

    Default Re: SQL query help requested

    Sorry. Just discovered a typo I did.

    From above,

    replace:

    PHP Code:
    WHERE customers_id ".(int)$_SESSION['customer_id']." AND date_purchased <> "2006-07-01 00:00:00" AND date_purchased "2006-08-01 00:00:00" 
    with:

    PHP Code:
    WHERE customers_id ".(int)$_SESSION['customer_id']." AND date_purchased "2006-07-01 00:00:00" AND date_purchased "2006-08-01 00:00:00" 

  3. #13
    Join Date
    Jul 2005
    Location
    Charlottesville, VA
    Posts
    431
    Plugin Contributions
    0

    Default Re: SQL query help requested

    hmm...

    someone at Lunarpages put this up for me, and it does get results, just not the results I need yet, heh. I think it just needs some tweaking.

    Code:
    SELECT zen1.`orders_id`, zen1.`customers_id`, zen1.`customers_name`, zen1.`date_purchased`, zen1.`order_total`
    FROM `zen_orders` AS zen1
    INNER JOIN `zen_orders` AS zen2 ON zen2.`customers_id` = zen1.`customers_id` AND zen2.`date_purchased` BETWEEN "2006-07-01 00:00:00" AND  "2006-08-01 00:00:00"
    INNER JOIN `zen_orders` AS zen3 ON zen3.`customers_id` = zen1.`customers_id` AND zen3.`date_purchased` < "2006-07-01 00:00:00"
    ORDER BY zen1.`date_purchased`;

  4. #14
    Join Date
    Jul 2005
    Location
    Charlottesville, VA
    Posts
    431
    Plugin Contributions
    0

    Default Re: SQL query help requested

    Thank you for your help, tried your suggestion:

    Code:
    SELECT customers_id, customers_name, date_purchased, order_total 
    FROM zen_orders
    WHERE customers_id = ".(int)$_SESSION['customer_id']." AND date_purchased > "2006-07-01 00:00:00" AND date_purchased < "2006-08-01 00:00:00" 
    ORDER BY date_purchased DESC;
    but this returns 0 rows.

    I need to compare customer_id for orders in July to customer_id in orders < July, to see if there are repeat customers in July.

    I need to store a selection of [customer_id] for purchase_date < July 1 in an alias, then left join? or inner join? to [selected order data] for purchase_date July 1 to July 30, and show all rows with customer_id matches.

  5. #15
    Join Date
    Aug 2004
    Posts
    1,590
    Plugin Contributions
    1

    Default Re: SQL query help requested

    Please post your entire project, as an attachment file, I will take a look at it. Otherwise, we might turn around in circle for a long time.

  6. #16
    Join Date
    Jan 2004
    Posts
    66,450
    Plugin Contributions
    81

    Default Re: SQL query help requested

    The lunarpages suggestion is the closest yet. How's this....?
    Code:
    select o.customers_id, o.customers_name, max(o2.date_purchased), count(o.orders_id) as counts
    from customers c
    LEFT JOIN orders o ON c.customers_id = o.customers_id AND o.date_purchased BETWEEN "2006-07-01 00:00:00" AND "2006-08-01 00:00:00"
    LEFT JOIN orders o2 ON c.customers_id = o2.customers_id AND o2.date_purchased < "2006-07-01 00:00:00"
    WHERE o.customers_id is not null and o2.date_purchased is not null
    group by c.customers_id
    ORDER BY counts desc, o.date_purchased DESC;
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

  7. #17
    Join Date
    Jul 2005
    Location
    Charlottesville, VA
    Posts
    431
    Plugin Contributions
    0

    Default Re: SQL query help requested

    Dr. Byte,

    This is getting close!!!

    I like your added feature to show the number of orders, but the number is inflated. For instance, in my test database, I placed 17 orders ever, but the result count shows as 66.

    Don't I want an inner join? I want all people who ordered in July, who previously ordered. It's just the overlap piece of two groups.

    TheOracle: my entire project is just this: a SQL query I can run once and a while in PHPMyAdmin. It isn't a PHP project.

    It's just to answer the question: "Who ordered in this month who ordered previously?"

    Sucky that I can't use sub-queries on this version of MySQL, that would make it much easier. LunarPages says that MySQL v. 4.0.25 is more stable in a shared hosting environment than 4.1. They offered to move my hosting to another server, but... that's another potential can of worms.

    ---Diana

  8. #18
    Join Date
    Jul 2005
    Location
    Charlottesville, VA
    Posts
    431
    Plugin Contributions
    0

    Default Re: SQL query help requested

    Here's what my hubby wrote (J2EE expert, they use MS SQL Server on their project.)

    This comes close, but gives me multiple lines of the same July transaction.

    Code:
    SELECT a.orders_id, a.customers_id, a.customers_name, a.date_purchased, a.order_total
    FROM zen_orders AS a
    INNER JOIN zen_orders AS b ON
        a.customers_id = b.customers_id AND
        b.date_purchased < "2006-07-01 00:00:00"
    WHERE a.date_purchased BETWEEN "2006-07-01 00:00:00" AND "2006-08-01 00:00:00"
    ORDER BY a.date_purchased;
    ---D

  9. #19
    Join Date
    Jul 2005
    Location
    Charlottesville, VA
    Posts
    431
    Plugin Contributions
    0

    Default Re: SQL query help requested

    This ended up doing the trick:

    Code:
    SELECT DISTINCT a.orders_id, a.customers_id, a.customers_name, a.date_purchased, a.order_total
    FROM zen_orders AS a
    INNER JOIN zen_orders AS b ON
        a.customers_id = b.customers_id AND
        b.date_purchased < "2006-07-01 00:00:00"
    WHERE a.date_purchased BETWEEN "2006-07-01 00:00:00" AND "2006-08-01 00:00:00"
    ORDER BY a.date_purchased;
    I like DrByte's idea of adding a count to show number of sales per customer, gonna try and work that out.

    Thanks to everyone who assisted on this; other ZC'ers might like this query, too.

    ---Diana

  10. #20
    Join Date
    Jul 2005
    Location
    Charlottesville, VA
    Posts
    431
    Plugin Contributions
    0

    Default Re: SQL query help requested

    This seems to accomplish the goal:
    Code:
    SELECT DISTINCT a.orders_id, a.customers_id, a.customers_name, 
    a.date_purchased, a.order_total, count(a.orders_id) AS "Number of Previous Orders"
    FROM zen_orders AS a
    INNER JOIN zen_orders AS b ON
        a.customers_id = b.customers_id AND
        b.date_purchased < "2006-07-01 00:00:00"
    WHERE a.date_purchased BETWEEN "2006-07-01 00:00:00" AND "2006-08-01 00:00:00"
    GROUP BY a.customers_id
    ORDER BY a.date_purchased
    LIMIT 0,100;

 

 
Page 2 of 2 FirstFirst 12

Similar Threads

  1. v154 Help with a SQL Query for Query Builder
    By lindasdd in forum Managing Customers and Orders
    Replies: 2
    Last Post: 24 Mar 2016, 01:18 PM
  2. sql insert query help
    By chuckb in forum General Questions
    Replies: 1
    Last Post: 27 Jul 2011, 09:59 PM
  3. SQL query help
    By petek in forum General Questions
    Replies: 6
    Last Post: 23 May 2010, 10:57 AM
  4. Help with a sql query
    By batteryman in forum General Questions
    Replies: 21
    Last Post: 3 Oct 2008, 11:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg