MySQL插入两个表并与引用表连接

时间:2013-11-25 17:31:46

标签: mysql sql database reference insert

您好我想将价格表中的数据添加到三个表产品表类别产品表类别表。下面是此图表的数据,您可以使用它来尝试它。之后我附上了我用来做这个的查询,但我认为这不是最好的解决方案。第一个查询仅向产品表添加新产品,第二个查询向类别表添加新类别,第三个查询用于向 category_product表添加数据。如果存在,你能帮助我并展示更好的解决方案吗?感谢

Database diagram

以下是创建和填充表格的数据:

CREATE TABLE `pricelist` (
  `id_pricelist` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `product_name` varchar(100) DEFAULT NULL,
  `product_category` varchar(100) DEFAULT NULL,
  `product_price` decimal(10,2) unsigned DEFAULT NULL,
  `product_pn` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id_pricelist`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;

INSERT INTO `pricelist` VALUES ('1', 'canon 200', 'Notebooks', '200.00', 'C200');
INSERT INTO `pricelist` VALUES ('2', 'HP 350', 'Notebooks', '150.00', 'HP350');
INSERT INTO `pricelist` VALUES ('3', 'Asus 300', 'Computers', '250.00', 'ABX');
INSERT INTO `pricelist` VALUES ('4', 'Asus 500', 'Computers', '175.00', 'ABAA');
INSERT INTO `pricelist` VALUES ('5', 'Asus 325', 'Printers', '120.00', 'CCA');
INSERT INTO `pricelist` VALUES ('6', 'HP Laser', 'Printers', '22.00', 'C#AA');
INSERT INTO `pricelist` VALUES ('7', 'Xerox CC', 'Scanners', '134.00', 'CCDD');


CREATE TABLE `categories` (
  `id_category` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `category_name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id_category`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;

INSERT INTO `categories` VALUES ('1', 'Notebooks');
INSERT INTO `categories` VALUES ('2', 'Cameras');
INSERT INTO `categories` VALUES ('3', 'Computers');


CREATE TABLE `product` (
  `id_product` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `product_name` varchar(100) DEFAULT NULL,
  `product_number` varchar(50) DEFAULT NULL,
  `insert_date` datetime DEFAULT NULL,
  PRIMARY KEY (`id_product`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=latin1;


INSERT INTO `product` VALUES ('1', 'asus 350', '350N', '2013-11-24 17:26:42');
INSERT INTO `product` VALUES ('2', 'canon 200', 'C200', '2013-11-24 17:26:52');
INSERT INTO `product` VALUES ('3', 'HP 350', 'HP350', '2013-11-24 17:26:56');
INSERT INTO `product` VALUES ('4', 'HP 450', 'HP450', '2013-11-24 17:26:59');


CREATE TABLE `category_product` (
  `id_category_product` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `fk_category` int(11) unsigned NOT NULL,
  `fk_product` int(11) unsigned NOT NULL,
  PRIMARY KEY (`id_category_product`),
  KEY `fk_product_category` (`fk_product`),
  KEY `fk_category_product` (`fk_category`),
  CONSTRAINT `fk_category_product` FOREIGN KEY (`fk_category`) REFERENCES `categories` (`id_category`),
  CONSTRAINT `fk_product_category` FOREIGN KEY (`fk_product`) REFERENCES `product` (`id_product`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=latin1;

INSERT INTO `category_product` VALUES ('1', '1', '1');
INSERT INTO `category_product` VALUES ('2', '1', '2');
INSERT INTO `category_product` VALUES ('3', '1', '3');
INSERT INTO `category_product` VALUES ('4', '1', '4');

插入新产品

INSERT INTO product
SELECT DISTINCT
    null,
    b.product_name,
    b.product_pn,
    NOW()
FROM
    product a
RIGHT JOIN pricelist b
ON a.product_number = b.product_pn
WHERE id_product IS NULL;

插入新类别

INSERT INTO categories
SELECT DISTINCT
    null,
    b.product_category
FROM
    categories a
RIGHT JOIN pricelist b
ON a.category_name = b.product_category
WHERE category_name IS NULL;

在产品表和分类表

之间插入参考表

这里我添加了条件,其中产品的插入日期必须等于今天所以当我想在一天中添加两次数据时这是问题

INSERT INTO category_product
SELECT DISTINCT
    null,
    c.id_category,
    a.id_product
FROM
    product a
JOIN pricelist b
ON a.product_number = b.product_pn
JOIN categories c
ON b.product_category = c.category_name
WHERE DATE(a.insert_date) = CURDATE();

0 个答案:

没有答案