内部加入不同的表格,根据列值
请帮我请我的选择。 我有我的表运营商,它包含关于承运人的信息:car_id,car_name,car_zones(这个值是表名,它有这个承运人的区数据),car_rates_exp(这个值是表名,它有这个承运人的出口费率数据), car_rates_imp(这个值是表名,它有这个载体的导入率数据),car_href(这个值是href跟踪这个载体的api)内部加入不同的表格,根据列值
另外我有表'orders',在所有字段中,它有列carrier_id ,from_country,weight和shipper_rate。我想做一个选择,我想在表中进行内部连接,但查询中的这个表名必须不同,基于carrier_id。我试图使这个查询,但它不工作:
SELECT o.id_order,
o.from_country,
c.firstname,
c.lastname,
o.carrier_id,
ot.name as order_type,
z.Zone_name,
cr.car_zones,
cr.car_name,
cr.car_href,
o.invoice_amount,
o.rated_weight,
o.shipping_rate,
o.shipping_rate_my_fee,
o.customs_tax,
o.customs_tax_2,
o.customs_my_fee,
o.total_shipping_fee,
o.total_my_costs,
o.1kg_price,
o.my_profit,
o.goods_for_ship,
o.waybill,
o.prev_DHL_fee,
o.customer_paid,
os.name as order_status,
os.color
FROM orders o
inner join customer c on o.id_customer = c.id
inner join order_types on on o.order_type = ot.id
inner join (select car_zones
from carriers
where car_id=o.carrier_id) z on o.from_country = z.id
inner join carriers cr on IF(o.carrier_id = 0, 1, o.carrier_id) = cr.car_id
inner join order_status os on o.order_status = os.id
where import_or_export = 'import' AND o.active > 0
因此字符串
inner join (select car_zones from carriers where car_id=o.carrier_id) z on o.from_country = z.id
不工作,可以请你帮我解决这种情况呢?
子查询不包含字段ID,其要加入到
o.from_country = z.id
的ID添加到子查询。
否的原因,您missunderstand我... 在此选择“(请从运营商那里car_zones = car_id o.carrier_id) - 我想唯一的价值car_zones这是表名,其中包含所有国家的ID。它们对于每个运营商都是不同的。所以我想用至极表名,我会做内部联接 – Vitaliy
它也喜欢选择t.order_name,t.car_id,r.country从表t,内连接的情况下t.car_id 如果1那么“DHL” 如果2那么“UPS”端 为r上t.id = r.car_id 的情况下功能将切换表名,取决于car_id,然后我们会得到表名,其中包含有关该载体的所有信息,并能加入它。 但由于这些制革工人将约10个,我想wtite thise代码,而不需要修改它,当一些新corriers将在未来增加 – Vitaliy
我删除了不兼容的标签。请添加您真正使用的数据库的标签。 –
在子查询中包含** id **为'(select_id,car_zones from carrier where car_id = o.carrier_id)z on o.from_country = z.id' – Viki888
在'FROM'子句中不能有子查询它引用'FROM'子句中的另一个表。你是否想要:o.carrier_id = z.car_id和o.from_country = z.id'上的inner join carriers z?如果不是,反而是什么? –