0

我有一个问题如何下订单产品,然后将订单插入连接的 5 个表中:一对多,这些表连接得如此紧密,以至于当客户进入房间时,下订单例如咖啡或水的产品,它必须显示在下订单的订单页面中,客户坐在哪个房间,然后服务员从状态获取订单是产品是否已付款。这些表格是:

CREATE TABLE IF NOT EXISTS `user` (
  `user_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `type_id` int(10) unsigned NOT NULL,
  `username` varchar(50) DEFAULT NULL,
  `password` varchar(32) DEFAULT NULL,
  `first_name` varchar(100) DEFAULT NULL,
  `last_name` varchar(100) DEFAULT NULL,
  `email` varchar(50) DEFAULT NULL,
  `picture` varchar(200) DEFAULT NULL,
  PRIMARY KEY (`user_id`),
  UNIQUE KEY `user_index1780` (`username`),
  KEY `user_FKIndex1` (`type_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=71 ;

CREATE TABLE IF NOT EXISTS `order` (
  `order_id` int(11) NOT NULL AUTO_INCREMENT,
  `time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `status` int(11) DEFAULT NULL,
  `room_id` int(11) NOT NULL,
  `user_id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`order_id`),
  KEY `fk_order_room1` (`room_id`),
  KEY `fk_order_user2` (`user_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;

CREATE TABLE IF NOT EXISTS `product` (
  `product_id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) DEFAULT NULL,
  `price` float DEFAULT NULL,
  `picture` varchar(500) DEFAULT NULL,
  PRIMARY KEY (`product_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;

CREATE TABLE IF NOT EXISTS `room` (
  `room_id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) DEFAULT NULL,
  `picture` varchar(450) DEFAULT NULL,
  `description` text,
  `user_id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`room_id`),
  KEY `fk_room_user1` (`user_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

CREATE TABLE IF NOT EXISTS `item_orders` (
  `order_id` int(11) NOT NULL,
  `product_id` int(11) NOT NULL,
  `quantity` int(11) DEFAULT NULL,
  PRIMARY KEY (`order_id`,`product_id`),
  KEY `fk_order_has_product_product1` (`product_id`),
  KEY `fk_order_has_product_order1` (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `type_user` (
  `type_id` int(10) unsigned NOT NULL,
  `name` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`type_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

现在,在我加入他们订购产品之后,什么都没有发生。

查询是:

SELECT order.order_id,
       user.first_name AS user,
       product.name,
       product.price,
       item_orders.quantity,
       product.price * item_orders.quantity AS sum,
       room.name,
       order.time,
       order.status
FROM user, product, room, `order`, item_orders
WHERE user.user_id = room.user_id
  AND order.room_id = room.room_id
  AND order.order_id = item_orders.order_id
  AND product.product_id = item_orders.product_id

这个连接很好,它只需要输入一个新的连接 order_id = '$order_id' 就可以了。

4

1 回答 1

0

如果您在查询引用的所有表中插入数据:

INSERT INTO type_user (type_id) VALUES (1);
INSERT INTO user (user_id, type_id, first_name) VALUES (1, 1, 'cappie');
INSERT INTO room (room_id, user_id, name) VALUES (10, 1, 'Room 10');
INSERT INTO `order` (order_id, room_id, user_id, status) VALUES (1 , 10, 1, 3);
INSERT INTO product (product_id, name, price) VALUES (100, 'Product A', 99.99);
INSERT INTO item_orders (order_id, product_id, quantity) VALUES (1, 100, 15);

您提供的查询(在评论中)将运行良好并返回(几乎)正确的结果

| ORDER_ID |   USER |      NAME |          PRICE | QUANTITY |               SUM |                          TIME | STATUS |
--------------------------------------------------------------------------------------------------------------------------
|        1 | cappie | Product A | 99.98999786377 |       15 | 1499.849967956543 | August, 08 2013 07:03:34+0000 |      3 |

您有一个浮点舍入错误,但这是另一天的故事

这是完整的SQL Fiddle

于 2013-08-08T07:21:15.873 回答