gpt4 book ai didi

MySQL 使用 pivot 从多个表中选择

转载 作者:太空宇宙 更新时间:2023-11-03 12:05:35 28 4
gpt4 key购买 nike

我在两个表之间有一个多对多的关系,我正在使用一个数据透视表来管理它。结构如下:

  • 表 1:汽车(car_id,...)
  • 表 2:品牌(brand_id,...)
  • 数据透视表:cars_brands(car_id、brand_id、单位、折扣)

仅使用一个查询,我试图选择某个品牌汽车的每个字段加上单位数量和折扣(数据透视表的两个字段...)

我的尝试:

SELECT c.*, cb.units, cb.discount 
FROM (cars c, cars_brands cb)
INNER JOIN cb ON c.car_id = cb.car_id
WHERE cb.brand_id = 1
ORDER BY c.car_id asc

我得到一个

#1066 - Not unique table/alias: 'cb'

MySQL 错误。

非常感谢任何帮助。

提前致谢。

最佳答案

我想你想要的是:

SELECT c.*, cb.units. cb.discount
FROM cars AS c
JOIN car_brands as cb ON cb.car_id = c.car_id
JOIN brands AS b ON b.brand_id = cb.brand_id
WHERE b.brand_name = 'Audi'
ORDER BY c.car_id ASC

关于MySQL 使用 pivot 从多个表中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26805905/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com