Mysql 如何使用其他表从基本参数获取项?
我开始使用数据库,出现了一个问题。 我正在尝试使用此查询按名称从product表中获取项。 我需要使用联接,因为我的表与其他表绑定 选择*从产品中,其中pName='45-DAVID'加入产品类别 product.product\u category\u id=product\u category.id加入 公司、制造商、产品 product.company\u manufacturer\u product\u id=company\u manufacturer\u product.id 但是当我得到这个错误:Mysql 如何使用其他表从基本参数获取项?,mysql,database,Mysql,Database,我开始使用数据库,出现了一个问题。 我正在尝试使用此查询按名称从product表中获取项。 我需要使用联接,因为我的表与其他表绑定 选择*从产品中,其中pName='45-DAVID'加入产品类别 product.product\u category\u id=product\u category.id加入 公司、制造商、产品 product.company\u manufacturer\u product\u id=company\u manufacturer\u product.id 但是当
error1064(42000):您的SQL语法有一个错误;检查与您的MySQL服务器版本对应的手册,以了解在第1行的“join product\u category on product.product\u category\u id=product\u category.id join”附近使用的正确语法
我的数据库脚本:
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
此外,尝试始终指定它是内部联接
还是左联接
,这样更容易阅读查询并快速理解查询
因此,对于您的查询:
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'