繁体   English   中英

如何使用其他表格通过参数从基础中获取商品?

[英]How can I get item from the base by parameter using additional tables?

我开始使用数据库,出现了一个问题。 我正在尝试使用此查询按名称从产品表中获取商品。 我需要使用JOIN,因为我的表与其他人联系在一起。

从产品WHERE pName = '45 -DAVID'中选择*在product.product_category_id = product_category.id上加入product_category在product.company_manufacturer_product_id = company_manufacturer_product.id上加入company_manufacturer_product

但是,当我收到此错误时: ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'join product_category on product.product_category_id = product_category.id join ' at line 1 ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'join product_category on product.product_category_id = product_category.id join ' at line 1

我的数据库脚本:

create table company_manufacturer_product(
  id int not null unique AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(30) not null unique
);

INSERT INTO company_manufacturer_product(id, name) VALUES (default, 'Adidas');
INSERT INTO company_manufacturer_product(id, name) VALUES (default, 'Collins');
INSERT INTO company_manufacturer_product(id, name) VALUES (default, 'Luis Vuitton');

create table product_category(
  id int not null unique AUTO_INCREMENT PRIMARY KEY,
  cName VARCHAR(30) not null unique
);

INSERT INTO product_category (id, cName) VALUES (default, 'jeans');
INSERT INTO product_category (id, cName) VALUES (default, 'sweatshirts');
INSERT INTO product_category (id, cName) VALUES (default, 'accessories');
INSERT INTO product_category (id, cName) VALUES (default, 'jackets');

create table product(
  id int not null unique AUTO_INCREMENT PRIMARY KEY,
  pName VARCHAR(30) not null,
  pSize VARCHAR(3) not null,
  price DECIMAL(6, 2) not null,
  color VARCHAR(30) not null,
  imageName VARCHAR(255) not null,
  company_manufacturer_product_id int not null,
  product_category_id int not null,
  KEY cmp_company_manufacturer_product_id(company_manufacturer_product_id),
  KEY pc_product_category_id(product_category_id),
  CONSTRAINT cmp_company_manufacturer_product_id FOREIGN KEY(company_manufacturer_product_id) references company_manufacturer_product(id),
  CONSTRAINT pc_product_category_id FOREIGN KEY(product_category_id) REFERENCES product_category(id)
);

INSERT INTO product (id, pName, pSize, price, color, imageName, company_manufacturer_product_id, product_category_id) VALUES (default, 'SST_TRACK_JACKET', 'M', 75.5, 'red', '1.png', 1, 4);
INSERT INTO product (id, pName, pSize, price, color, imageName, company_manufacturer_product_id, product_category_id) VALUES (default, '45-DAVID', 'L', 100.00, 'blue', '2.png', 2, 1);
INSERT INTO product (id, pName, pSize, price, color, imageName, company_manufacturer_product_id, product_category_id) VALUES (default, 'ALLIANCE-SUNGLASSES', 'S', 810.00, 'black', '3.png', 3, 3);
INSERT INTO product (id, pName, pSize, price, color, imageName, company_manufacturer_product_id, product_category_id) VALUES (default, 'SWEATSHIRTS-228', 'XL', 120.20, 'blue', '4.png', 2, 2);
INSERT INTO product (id, pName, pSize, price, color, imageName, company_manufacturer_product_id, product_category_id) VALUES (default, 'TREFOIL HOODIE', 'M', 75.10, 'white', 'TREFOIL_HOODIE.png', 1, 2);
INSERT INTO product (id, pName, pSize, price, color, imageName, company_manufacturer_product_id, product_category_id) VALUES (default, 'SPLIT OVERSIZED JACKET', 'L', 2930.30, 'grey', 'SPLIT_OVERSIZED_JACKET.png', 3, 4);
INSERT INTO product (id, pName, pSize, price, color, imageName, company_manufacturer_product_id, product_category_id) VALUES (default, 'PROCESS_SP1 WATCH', 'S', 75.0, 'red', 'PROCESS_SP1_WATCH.png', 1, 3);
INSERT INTO product (id, pName, pSize, price, color, imageName, company_manufacturer_product_id, product_category_id) VALUES (default, 'CJEAN30190', 'XL', 69.50, 'blue', 'CJEAN30190.png', 2, 1);
INSERT INTO product (id, pName, pSize, price, color, imageName, company_manufacturer_product_id, product_category_id) VALUES (default, 'NEON MONOGRAM SWEATER', 'M', 765.50, 'neon', 'NEON_MONOGRAM_SWEATER.png', 3, 4);

我在做什么错,我该如何解决? 如果我没有指定参数,那么一切都会很好。

有关查询和SQL的某些基础可能很容易阅读。

查询总是像这样:

SELECT
  MyCols
FROM 
 MyTable1
JOIN
 OtherTable2
WHERE
 myCondition

此外,尝试始终指定它是INNER JOIN还是LEFT JOIN ,这样更容易阅读查询并快速理解它。

因此,对于您的查询:

SELECT * 
FROM product 
INNER JOIN product_category on product.product_category_id = product_category.id
INNER JOIN company_manufacturer_product
   on product.company_manufacturer_product_id=company_manufacturer_product.id
WHERE pName='45-DAVID'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM