如何在mysql中连接3个表

cwdobuhd  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(373)


嗨,我需要你的帮助。
我有这个数据库架构,我想做一个“选择”返回所有“产品”,其中brands.brand\u slug=“acer”和categories.category\u slug=“laptop”。例如,我想要一个由宏碁在笔记本电脑类别中生产的所有产品的列表。
我试过用这个命令行,但很明显我收到了所有制造商生产的所有笔记本电脑的列表。。。
我该怎么办?

SELECT products.product_slug,
products.product_description,
products.product_img,
products.product_is_on_top,
products.product_title
FROM products
LEFT OUTER JOIN categories ON products.category=categories.category_slug
LEFT OUTER JOIN brand_category ON categories.category_slug=brand_category.category_slug
LEFT OUTER JOIN brands ON brand_category.brand_slug=brands.brand_slug
WHERE brands.brand_slug= "acer"
AND categories.category_slug="laptop"
mbjcgjjk

mbjcgjjk1#

试试这个

SELECT products.product_slug,
products.product_description,
products.product_img,
products.product_is_on_top,
products.product_title
FROM products
INNER JOIN categories ON products.category=categories.category_slug
INNER JOIN brand_category ON categories.category_slug=brand_category.category_slug
INNER JOIN brands ON brand_category.brand_slug=brands.brand_slug
WHERE brands.brand_slug= 'acer'
AND categories.category_slug='laptop'
c90pui9n

c90pui9n2#

SELECT 
    orderNumber,
    orderDate,
    orderLineNumber,
    productName,
    quantityOrdered,
    priceEach
FROM
    orders
INNER JOIN
    orderdetails USING (orderNumber)
INNER JOIN
    products USING (productCode)
ORDER BY 
    orderNumber, 
    orderLineNumber;

相关问题