Solved

Getting the correct order history records

Posted on 2010-09-15
12
394 Views
Last Modified: 2012-08-13
When I change the id_order_state in ps_order_history it adds another record
instead of changing the current id_order_history. I need to obtain the list of the
records in ps_order_history where the highest the id_order numbers = the id_order
where the id_order_state = 12.

For example, in this table, the first record has an id_order_state of 12. But in
subsequent updates the status has been updated to a 2, I want only the records where
there hasn't been an update. Did I make that clear enough?
(Table definitions and data listed below)

Here is my original SQL, but it is always returning id_order_history 1 even when it
has been updated. Actually, I'm not even sure this is the most efficient way to do this.
Please don't berate me for my SQL, I'm nt that bright on JOINS, but, please, make
suggestions for improvement.

SELECT o.id_order, 
       osl.`name`
FROM `ps_order_history` oh
INNER JOIN `ps_orders` o 
  ON ( o.`id_order` = oh.`id_order` )
INNER JOIN `ps_order_detail` od 
  ON od.`id_order` = o.`id_order`
LEFT JOIN `ps_order_state` os 
  ON os.`id_order_state` = oh.`id_order_state`
LEFT JOIN `ps_order_state_lang` osl 
  ON (osl.`id_order_state` = oh.`id_order_state` AND osl.`id_lang` = 1)
WHERE osl.`name` = 'Generation Error'

Open in new window


CREATE TABLE IF NOT EXISTS `ps_orders` (
  `id_order` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `id_carrier` int(10) unsigned NOT NULL,
  `id_lang` int(10) unsigned NOT NULL,
  `id_customer` int(10) unsigned NOT NULL,
  `id_cart` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id_order`),
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

INSERT INTO `ps_orders` (`id_order`, `id_carrier`, `id_lang`, 
                         `id_customer`, `id_cart`) 
                      VALUES (1, 2, 1, 2, 1);

CREATE TABLE IF NOT EXISTS `ps_order_history` (
  `id_order_history` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `id_employee` int(10) unsigned NOT NULL,
  `id_order` int(10) unsigned NOT NULL,
  `id_order_state` int(10) unsigned NOT NULL,
  `date_add` datetime NOT NULL,
  PRIMARY KEY (`id_order_history`),
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

INSERT INTO `ps_order_history` (`id_order_history`, `id_employee`, 
                                `id_order`, `id_order_state`, 
                                `date_add`) 
                         VALUES (1, 0, 1, 12, '2010-09-14 11:19:04'), 
                                (2, 0, 1, 2, '2010-09-15 19:31:25'), 
                                (3, 0, 1, 2, '2010-09-15 19:33:33'), 
                                (4, 0, 1, 2, '2010-09-15 19:36:56'), 
                                (5, 0, 1, 2, '2010-09-15 19:37:40'), 
                                (6, 0, 1, 2, '2010-09-15 19:41:03'), 
                                (7, 0, 1, 2, '2010-09-15 19:41:35');

CREATE TABLE IF NOT EXISTS `ps_order_state` (
  `id_order_state` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `invoice` tinyint(1) unsigned DEFAULT '0',
  PRIMARY KEY (`id_order_state`)
) ENGINE=MyISAM AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;

INSERT INTO `ps_order_state` (`id_order_state`, `invoice`) 
                      VALUES (1, 0), (2, 1), (3, 1), (4, 1), 
                             (5, 1), (6, 0), (7, 1), (8, 0), 
                             (9, 1), (10, 0),(11, 0),(12, 0);

CREATE TABLE IF NOT EXISTS `ps_order_state_lang` (
  `id_order_state` int(10) unsigned NOT NULL,
  `name` varchar(64) NOT NULL,
  `template` varchar(64) NOT NULL,
  UNIQUE KEY `order_state_lang_index` (`id_order_state`,`id_lang`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO `ps_order_state_lang` (`id_order_state`, `name`, 
                                   `template`) 
           VALUES (1, 'Awaiting cheque payment', 'cheque'), 
                  (2, 'Payment accepted', 'payment'), 
                  (3, 'Preparation in progress', 'preparation'), 
                  (4, 'Shipped', 'shipped'), 
                  (5, 'Delivered', ''), 
                  (6, 'Canceled', 'order_canceled'), 
                  (7, 'Refund', 'refund'), 
                  (8, 'Payment error', 'payment_error'), 
                  (9, 'On backorder', 'outofstock'), 
                  (10, 'Awaiting bank wire payment', 'bankwire'), 
                  (11, 'Awaiting PayPal payment', ''), 
                  (12, 'Generation Error', 'download_product_error');

Open in new window

0
Comment
Question by:EddieShipman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 33687257
did you mean to type o.`id_lang` = 1 on Line 11?
SELECT o.id_order, 
       osl.`name`
FROM `ps_order_history` oh
INNER JOIN `ps_orders` o 
  ON ( o.`id_order` = oh.`id_order` )
INNER JOIN `ps_order_detail` od 
  ON od.`id_order` = o.`id_order`
LEFT JOIN `ps_order_state` os 
  ON os.`id_order_state` = oh.`id_order_state`
LEFT JOIN `ps_order_state_lang` osl 
  ON (osl.`id_order_state` = oh.`id_order_state` AND o.`id_lang` = 1)
WHERE osl.`name` = 'Generation Error'

Open in new window

0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 33687275
you had osl.`id_lang` = 1 which isn't a valid column. I think you meant to type o.`id_lang` = 1 instead. Line 11
0
 
LVL 26

Author Comment

by:EddieShipman
ID: 33687630
Yes, I neglected to fix that in the query that I posted. The table actually has that column.
0
 Watch the Recording: Learning MySQL 5.7

MySQL 5.7 has a lot of new features. If you've dabbled with an older version of MySQL, it is definitely worth learning.

 
LVL 26

Author Comment

by:EddieShipman
ID: 33692188
Ok, let me repost how this should look...

When I change the id_order_state in ps_order_history it adds another record instead of changing the current id_order_history. I need to obtain the list of the records in ps_order_history where the highest the id_order numbers = the id_order where the id_order_state = 12.

For example, in the ps_order_history table, the first record has an id_order_state of 12. But the status for id_order 1 has been updated to a 2, I want only the records where there hasn't been an update. I should only get the record for id_order 2. Did I make that clear enough?
(Table definitions and data listed below)

Here is my original SQL, but it is always returning id_order_history 1 even when it has been updated. Actually, I'm not even sure this is the most efficient way to do this. Please don't berate me for my SQL, JOINS are not one of my strong suits but, please, make suggestions for improvement.

SELECT o.id_order, 
       osl.`name`
FROM `ps_order_history` oh
INNER JOIN `ps_orders` o 
  ON ( o.`id_order` = oh.`id_order` )
INNER JOIN `ps_order_detail` od 
  ON od.`id_order` = o.`id_order`
LEFT JOIN `ps_order_state` os 
  ON os.`id_order_state` = oh.`id_order_state`
LEFT JOIN `ps_order_state_lang` osl 
  ON (osl.`id_order_state` = oh.`id_order_state` AND osl.`id_lang` = 1)
WHERE osl.`name` = 'Generation Error'

CREATE TABLE IF NOT EXISTS `ps_orders` (
  `id_order` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `id_carrier` int(10) unsigned NOT NULL,
  `id_lang` int(10) unsigned NOT NULL,
  `id_customer` int(10) unsigned NOT NULL,
  `id_cart` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id_order`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

INSERT INTO `ps_orders` (`id_order`, `id_carrier`, `id_lang`, 
                         `id_customer`, `id_cart`) 
                      VALUES (1, 2, 1, 2, 1),
                             (2, 2, 1, 2, 2);

CREATE TABLE IF NOT EXISTS `ps_order_history` (
  `id_order_history` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `id_employee` int(10) unsigned NOT NULL,
  `id_order` int(10) unsigned NOT NULL,
  `id_order_state` int(10) unsigned NOT NULL,
  `date_add` datetime NOT NULL,
  PRIMARY KEY (`id_order_history`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

INSERT INTO `ps_order_history` (`id_order_history`, `id_employee`, 
                                `id_order`, `id_order_state`, 
                                `date_add`) 
                         VALUES (1, 0, 1, 12, '2010-09-14 11:19:04'), 
                                (2, 0, 1, 2, '2010-09-15 19:31:25'), 
                                (3, 0, 2, 12, '2010-09-15 19:37:40');

CREATE TABLE IF NOT EXISTS `ps_order_state` (
  `id_order_state` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `invoice` tinyint(1) unsigned DEFAULT '0',
  PRIMARY KEY (`id_order_state`)
) ENGINE=MyISAM AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;

INSERT INTO `ps_order_state` (`id_order_state`, `invoice`) 
                      VALUES (1, 0), (2, 1), (3, 1), (4, 1), 
                             (5, 1), (6, 0), (7, 1), (8, 0), 
                             (9, 1), (10, 0),(11, 0),(12, 0);

CREATE TABLE IF NOT EXISTS `ps_order_state_lang` (
  `id_order_state` int(10) unsigned NOT NULL,
  `name` varchar(64) NOT NULL,
  `template` varchar(64) NOT NULL,
  `id_lang` INT(10) UNSIGNED NOT NULL
  UNIQUE KEY `order_state_lang_index` (`id_order_state`,`id_lang`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO `ps_order_state_lang` (`id_order_state`, `name`, 
                                   `id_lang`, `template`) 
           VALUES (1, 'Awaiting cheque payment', 1, 'cheque'), 
                  (2, 'Payment accepted', 1, 'payment'), 
                  (3, 'Preparation in progress', 1, 'preparation'), 
                  (4, 'Shipped', 1, 'shipped'), 
                  (5, 'Delivered', 1, ''), 
                  (6, 'Canceled', 1, 'order_canceled'), 
                  (7, 'Refund', 1, 'refund'), 
                  (8, 'Payment error', 1, 'payment_error'), 
                  (9, 'On backorder', 1, 'outofstock'), 
                  (10, 'Awaiting bank wire payment', 1, 'bankwire'), 
                  (11, 'Awaiting PayPal payment', 1, ''), 
                  (12, 'Generation Error', 1, 'download_product_error');

Open in new window

0
 
LVL 26

Author Comment

by:EddieShipman
ID: 33692376
OOPS, need to remove this line:

INNER JOIN `ps_order_detail` od
  ON od.`id_order` = o.`id_order`
0
 
LVL 26

Author Comment

by:EddieShipman
ID: 33692397
Also, need a comma after this line, in create code for ps_order_state_lang:
 `id_lang` INT(10) UNSIGNED NOT NULL
0
 
LVL 26

Author Comment

by:EddieShipman
ID: 33692620
Well, after getting my dummy data situated correctly and playing with it a while.
I came up with the SQL below. It returns the data I need but is it the most optimal?
BTW, it includes some other tables and columns that aren't in the dummy tables/data I posted above.
SELECT o.id_order
     , CONCAT( LEFT( c.`firstname` , 1 ) , ', ', c.`lastname` ) AS `customer`
     , osl.`name`
     , p.reference
     , pd.physically_filename as filename
FROM `ps_order_history` oh
INNER JOIN `ps_orders` o ON ( o.`id_order` = oh.`id_order` )
LEFT JOIN `ps_customer` c ON ( c.`id_customer` = o.`id_customer` )
INNER JOIN `ps_order_detail` od ON od.`id_order` = o.`id_order`
LEFT JOIN `ps_order_state` os ON os.`id_order_state` = oh.`id_order_state`
LEFT JOIN `ps_order_state_lang` osl ON (osl.`id_order_state` = oh.`id_order_state` AND osl.`id_lang` = 1)
LEFT JOIN `ps_product` p ON p.`id_product` = od.`product_id`
INNER JOIN ps_product_download pd ON pd.`id_product` = p.`id_product`
WHERE oh.`id_order` NOT IN (SELECT `id_order` 
                            FROM `ps_order_history` 
                            WHERE `id_order_state` <> 12)

Open in new window

0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 33696945
everything looks excellent accept for the WHERE.
Since you are working with `ps_order_history` AKA oh, you can specify directly that you want to return only `id_order_state` = 12
SELECT o.id_order
     , CONCAT( LEFT( c.`firstname` , 1 ) , ', ', c.`lastname` ) AS `customer`
     , osl.`name`
     , p.reference
     , pd.physically_filename as filename
FROM `ps_order_history` oh
INNER JOIN `ps_orders` o ON ( o.`id_order` = oh.`id_order` )
LEFT JOIN `ps_customer` c ON ( c.`id_customer` = o.`id_customer` )
INNER JOIN `ps_order_detail` od ON od.`id_order` = o.`id_order`
LEFT JOIN `ps_order_state` os ON os.`id_order_state` = oh.`id_order_state`
LEFT JOIN `ps_order_state_lang` osl ON (osl.`id_order_state` = oh.`id_order_state` AND osl.`id_lang` = 1)
LEFT JOIN `ps_product` p ON p.`id_product` = od.`product_id`
INNER JOIN ps_product_download pd ON pd.`id_product` = p.`id_product`
WHERE `id_order_state` = 12

Open in new window

0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 33696956
Also, If you are planning to "group by" you can replace WHERE with HAVING when you need to.
0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 33696981
possibly for even more speed we can subquery the `id_order_state` = 12:
SELECT o.id_order
     , CONCAT( LEFT( c.`firstname` , 1 ) , ', ', c.`lastname` ) AS `customer`
     , osl.`name`
     , p.reference
     , pd.physically_filename as filename
FROM 
(
     SELECT id_order, oh.`id_order_state`
     FROM `ps_order_history` 
     WHERE `id_order_state` = 12
) oh
INNER JOIN `ps_orders` o ON ( o.`id_order` = oh.`id_order` )
LEFT JOIN `ps_customer` c ON ( c.`id_customer` = o.`id_customer` )
INNER JOIN `ps_order_detail` od ON od.`id_order` = o.`id_order`
LEFT JOIN `ps_order_state` os ON os.`id_order_state` = oh.`id_order_state`
LEFT JOIN `ps_order_state_lang` osl ON (osl.`id_order_state` = oh.`id_order_state` AND osl.`id_lang` = 1)
LEFT JOIN `ps_product` p ON p.`id_product` = od.`product_id`
INNER JOIN ps_product_download pd ON pd.`id_product` = p.`id_product`

Open in new window

0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 33696993
ignore #33696981 there are errors.

fixed:
SELECT o.id_order
     , CONCAT( LEFT( c.`firstname` , 1 ) , ', ', c.`lastname` ) AS `customer`
     , osl.`name`
     , p.reference
     , pd.physically_filename as filename
FROM 
(
     SELECT id_order, `id_order_state`
     FROM `ps_order_history` 
     WHERE `id_order_state` = 12
) oh
INNER JOIN `ps_orders` o ON ( o.`id_order` = oh.`id_order` )
LEFT JOIN `ps_customer` c ON ( c.`id_customer` = o.`id_customer` )
INNER JOIN `ps_order_detail` od ON od.`id_order` = o.`id_order`
LEFT JOIN `ps_order_state` os ON os.`id_order_state` = oh.`id_order_state`
LEFT JOIN `ps_order_state_lang` osl ON (osl.`id_order_state` = oh.`id_order_state` AND osl.`id_lang` = 1)
LEFT JOIN `ps_product` p ON p.`id_product` = od.`product_id`
INNER JOIN ps_product_download pd ON pd.`id_product` = p.`id_product`

Open in new window

0
 
LVL 19

Accepted Solution

by:
NerdsOfTech earned 500 total points
ID: 33696999
fixed again :0

subquery version:
SELECT o.id_order
     , CONCAT( LEFT( c.`firstname` , 1 ) , ', ', c.`lastname` ) AS `customer`
     , osl.`name`
     , p.reference
     , pd.physically_filename as filename
FROM 
(
     SELECT `id_order`, `id_order_state`
     FROM `ps_order_history` 
     WHERE `id_order_state` = 12
) oh
INNER JOIN `ps_orders` o ON ( o.`id_order` = oh.`id_order` )
LEFT JOIN `ps_customer` c ON ( c.`id_customer` = o.`id_customer` )
INNER JOIN `ps_order_detail` od ON od.`id_order` = o.`id_order`
LEFT JOIN `ps_order_state` os ON os.`id_order_state` = oh.`id_order_state`
LEFT JOIN `ps_order_state_lang` osl ON (osl.`id_order_state` = oh.`id_order_state` AND osl.`id_lang` = 1)
LEFT JOIN `ps_product` p ON p.`id_product` = od.`product_id`
INNER JOIN ps_product_download pd ON pd.`id_product` = p.`id_product`

Open in new window

0

Featured Post

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question