我有一个问题如何下订单产品,然后将订单插入连接的 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' 就可以了。