MySQL:连接3个表不会';t返回第三次联接的所有结果

MySQL:连接3个表不会';t返回第三次联接的所有结果,mysql,Mysql,目标:当给定用户id时,检索最后2个订单、付款信息以及订单中的所有项目 查询: SELECT PO.id, PO.id AS order_id, PO.user_id, PO.deliveryaddress_id, PO.delivery_type, PO.store_id, PO

目标:当给定
用户id
时,检索最后2个订单、付款信息以及订单中的所有项目

查询:

SELECT  
            PO.id, 
            PO.id                   AS order_id, 
            PO.user_id, 
            PO.deliveryaddress_id, 
            PO.delivery_type, 
            PO.store_id,
            PO.placed_by, 
            PO.orderplaced_ts       AS order_timestamp,
            POP.subtotal            AS order_subtotal, 
            POP.tax                 AS order_tax, 
            POP.secondary_tax       AS order_secondary_tax, 
            POP.soda_tax            AS order_soda_tax, 
            POP.delivery_fee        AS order_delivery_fee, 
            POP.tip                 AS order_tip, 
            POP.discount            AS order_discount, 
            POP.total               AS order_total,
            POP.payment_method, 
            POP.payment_details,
            POM.*
          FROM `orders` AS PO 
          JOIN `order_payments` AS POP 
            ON PO.id = POP.order_id
          JOIN`order_items` AS POM 
            ON PO.id = POM.order_id
          WHERE 
            PO.user_id = 12345
          AND 
            PO.order_status != 'Cancelled'
          AND
            PO.order_status != 'Pending'
          AND 
            POP.payment_collected = '1'
          GROUP BY PO.id DESC
我当前收到的结果只是从
order\u items
表中检索第一条记录,这是不正确的。我正在尝试从
order\u项目中检索所有行

这是数据集

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 INTO `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 is a special request', '{\"45\":1,\"80\":1,\"93\":1}', 14.86, 0);

INSERT INTO `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 INTO `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');

和SQLFIDLE相同:

尝试用ORDER BY替换聚合(GROUP BY)。目前,您按po.id进行聚合,这对每个订单都是唯一的。

结果会是什么样的按移动
分组
您没有执行任何需要分组的操作是的,而且我在PDO中也犯了一个错误,fetch GROUP导致行也被分组。-谢谢