协助SELECT

时间:2011-08-27 16:36:59

标签: mysql database-relations

我有三个表:hosting_pack,hosting_attr和hosting_attr_value。

一个hosting_category可以有许多hosting_packs和许多hosting_attributes。

要知道hosting_value,我们必须知道hosting_pack和hosting_attribute,所以hosting_pack包含包信息,hosting_attr包含属性,hosting_attr_value包含一个包中每个属性的值。

描述如下:

enter image description here

我想显示一个这样的表:

hosting_attr,          hosting_attr_value ,           for a given id_hosting_pack

注意:hosting_categ和hosting_attr之间的关系不存在,只需忽略它。

3 个答案:

答案 0 :(得分:1)

试试这个:

select hosting_attr.attr_label, hosting_attr_value.hosting_attr_value from hosting_attr_value, hosting_attr where hosting_attr_value.id_hosting_pack = ? and hosting_attr.id_hosting_attr = hosting_attr_value.id_hosting_attr

如果您不知道id_hosting_pack,可以在线查找

select hosting_attr.attr_label, hosting_attr_value.hosting_attr_value from hosting_attr_value, hosting_attr where hosting_attr_value.id_hosting_pack = (select id_hosting_pack from hosting_pack where pack_name = ?) and hosting_attr.id_hosting_attr = hosting_attr_value.id_hosting_attr

答案 1 :(得分:1)

select ha.attr_label, hav.attr_value from hosting_attr ha
inner join hosting_attr_value hav on ha.id_hosting_attr = hav.id_hosting_attr
where hav.id_hosting_pack = :theIdOfTheHostingPack

答案 2 :(得分:1)

尝试:

select *
from 
 hosting_pack hp,
 hosting_attr_value hav,
 hosting_attr ha
where
    hp.id_hosting_pack = hav.id_hosting_pack
and hav.id_hosting_attr = ha.id_hosting_attr
是吗?

还尝试稍微研究一下SQL。