在MySQL查询中苦苦挣扎

时间:2013-03-04 03:36:53

标签: mysql foreign-keys

我有以下型号:

enter image description here

我已经在这一段时间了,仍然不知道如何妥善解决它。已经看过加入两个别名的子查询,连接两个视图,并尝试了一个gynormous和丑陋的一体化查询,其中没有一个工作。

我的问题很简单:

如何选择deivce_names.name并将它们与模型和制造商匹配?

3 个答案:

答案 0 :(得分:1)

SELECT
    name, manufacturer, model
FROM
    device_names
    JOIN devices ON (device_names.id = name_id)
    JOIN devices_generic ON (generic_device_id = devices_generic.id)
    JOIN device_manufacturers ON (manufacturer_id = device_manufacturers.id)
    JOIN device_models ON (model_id = device_models.id)

答案 1 :(得分:0)

select
    device_names.name as device_name,
    device_manufacturers.name as device_manufacturer_name,
    device_models.name as device_model_name
from deivce_names
join device on device.name_id = deivce_names.id
join devices_generic on devices_generic.id = devices.generic_device_id
join device_manufacturers on devices_generic.manufacturer_id = device_manufacturers.id
join device_models on device_models.id = devices_generic.model_id

答案 2 :(得分:0)

SELECT dm1.manufacturer, dm2.model from device_manufacturers as dm1 join devices_generic as dg on dg.manufacturer_id = dm1.id join device_models as dm2 on dm2.id = dg.model_id join devices as d2 on d2.generic_device_id = dg.id join device_names as dn on dn.id = d2.name_id where name = 'foo'

将来,您可能只想将数据放入fiddle并在那里播放。