MySQL: Joining 3 tables doesn't return all results from 3rd Join -
goal: when given user_id
, retrieve last 2 orders, payment information, , items inside order.
query:
select po.id, po.id order_id, po.user_id, po.deliveryaddress_id, po.delivery_type, po.store_id, po.placed_by, po.orderplaced_ts order_timestamp, pop.subtotal order_subtotal, pop.tax order_tax, pop.secondary_tax order_secondary_tax, pop.soda_tax order_soda_tax, pop.delivery_fee order_delivery_fee, pop.tip order_tip, pop.discount order_discount, pop.total order_total, pop.payment_method, pop.payment_details, pom.* `orders` po join `order_payments` pop on po.id = pop.order_id join`order_items` pom on po.id = pom.order_id po.user_id = 12345 , po.order_status != 'cancelled' , po.order_status != 'pending' , pop.payment_collected = '1' group po.id desc
the result i'm current receiving retrieving first record order_items
table, isn't correct. i'm attempting retrieve rows order_items
here data set
create table `orders` ( `id` int(11) not null auto_increment, `user_id` int(11) not null, `deliveryaddress_id` int(11) not null, `billingaddress_id` int(11) not null, `delivery_type` enum('d','p') not null comment 'd: delivery; p: pickup;', `store_id` int(11) not null, `placed_by` int(11) not null, `ordercreated_ts` datetime not null, `orderplaced_ts` datetime not null, `orderread_ts` datetime not null, `orderfulfilled_ts` datetime not null, `order_status` enum('','cancelled','pending') not null default '', primary key (`id`), key `user_id` (`user_id`) ) engine=myisam auto_increment=5443062 default charset=latin1; create table `order_payments` ( `id` int(11) not null auto_increment, `user_id` int(11) not null, `order_id` int(11) not null, `subtotal` decimal(9,2) not null, `tax` decimal(9,2) not null, `secondary_tax` decimal(9,2) not null, `soda_tax` decimal(9,2) not null, `delivery_fee` decimal(9,2) not null, `tip` decimal(9,2) not null, `discount` decimal(9,2) not null, `total` decimal(9,2) not null, `payment_method` enum('level up','credit card','cash','house account') not null, `payment_details` varchar(150) default null, `payment_collected` enum('0','1') not null default '0' comment '0: payment not collected; 1: payment collected;', primary key (`id`), key `order_id` (`order_id`), key `user_id` (`user_id`) ) engine=myisam auto_increment=5277852 default charset=latin1; create table `order_items` ( `id` int(11) not null auto_increment, `order_id` int(11) not null, `menuitem_id` int(11) not null, `quantity` int(11) not null, `whofor` varchar(50) default null, `choppingpreference` varchar(50) default null, `in_a_wrap` varchar(50) default null, `dressingpreference` varchar(50) default null, `includebread` tinyint(1) default null, `specialrequest` text, `customizations` text, `price` decimal(9,2) not null, `fav_id` int(11) default null, primary key (`id`), key `order_id` (`order_id`) ) engine=myisam auto_increment=9647592 default charset=latin1; insert `order_items` (`id`, `order_id`, `menuitem_id`, `quantity`, `whofor`, `choppingpreference`, `in_a_wrap`, `dressingpreference`, `includebread`, `specialrequest`, `customizations`, `price`, `fav_id`) values (9647591, 5443021, 451, 1, '', '', null, '', 0, '', '[]', 1.99, 0), (9647581, 5443021, 43, 1, 'alex', 'yes', null, 'yes', 0, 'this special request', '{\"45\":1,\"80\":1,\"93\":1}', 14.86, 0); insert `orders` (`id`, `user_id`, `deliveryaddress_id`, `billingaddress_id`, `delivery_type`, `store_id`, `placed_by`, `ordercreated_ts`, `orderplaced_ts`, `orderread_ts`, `orderfulfilled_ts`, `order_status`) values (5443021, 12345, 0, 0, 'd', 6, 0, '2017-08-17 16:35:15', '2017-08-17 16:36:13', '0000-00-00 00:00:00', '0000-00-00 00:00:00', ''); insert `order_payments` (`id`, `user_id`, `order_id`, `subtotal`, `tax`, `secondary_tax`, `soda_tax`, `delivery_fee`, `tip`, `discount`, `total`, `payment_method`, `payment_details`, `payment_collected`) values (5277851, 0, 5443021, 16.85, 1.50, 0.00, 0.00, 1.99, 3.03, 0.00, 20.34, 'credit card', '1647057284', '1');
and sqlfiddle of same: http://www.sqlfiddle.com/#!9/89024b/7
try replace aggregation (group by) order by. aggregate po.id unique each order.
Comments
Post a Comment