2

我有一个像这样的值表:

TABLE:     prop_feat
TYPE       FEAT
Appliances Gas Range
Appliances Fridge
Interior   Hardwood Flooring

我正在尝试使用以下功能显示一次类型:

家电

煤气灶、冰箱

内部的

硬木地板

这些值可能是空的,所以我需要 LEFT JOIN 这个表。我到处找了!我应该如何编写 MySQL 来显示这样的结果?这是我到目前为止所拥有的,但还没有完全做到。(我知道.. 不是 LEFT JOIN.. )

SELECT *
  (SELECT GROUP_CONCAT( DISTINCT CONCAT ('', type, '', prop_feat.feat, '') 
   ORDER BY type ASC SEPARATOR ' '  )
FROM prop_feat
WHERE prop_feat.id_prop = '$page'  )  AS feat
FROM properties
WHERE id_pg = '$page'

任何帮助将不胜感激!

编辑

这是我的全部查询.. 我有一大堆连接。这使得类型/功能问题变得困难。

SELECT *,
GROUP_CONCAT(CONCAT('<tr><td>', style, '</td><td>', beds, '</td><td>', ba, '</td><td>', sq_ft, '</td><td>', rent, '</td><td>', dep, '</td></tr>') 
    ORDER BY beds DESC SEPARATOR '') AS unit, 
(SELECT 
    GROUP_CONCAT( DISTINCT CONCAT ('<h4>', type, '</h4><div>', prop_feat.feat, '</div>') 
        ORDER BY type ASC SEPARATOR ' '  )
    FROM prop_feat
    WHERE prop_feat.id_prop = '$page'  )  AS interior 
FROM properties 
INNER JOIN prop_units ON prop_units.id_prop = '$page'
LEFT JOIN prop_intro ON prop_intro.id_prop = '$page'  
LEFT JOIN prop_hours ON prop_hours.id_prop = '$page'  
WHERE id_pg = '$page'
GROUP BY type

属性有:名称、地址、城市、省、邮政编码、电话(显示一个属性 - 基本信息)
Prop_units 有:床、浴室、平方英尺、租金、押金、样式
Prop_intro:标题、文本(地点描述)
Prop_hours:Sunfrom , Sunto, MonFrom, MonTo, etc (times open in week)
Prop_feat: 有硬木地板等房产特征,只需要按类型排序和显示类型一次..如果可能的话!

编辑编辑 - 现在使用这个

$info = mysql_fetch_array(mysql_query("SELECT *,
GROUP_CONCAT(CONCAT('<tr><td>', style, '</td><td>', beds, '</td><td>', ba, '</td><td>', sq_ft, '</td><td>$', rent, '</td><td>',IF(dep='','','$'), dep, '</td></tr>') 
    ORDER BY u_order ASC SEPARATOR '') AS unit
FROM properties 
INNER JOIN prop_units ON prop_units.id_prop = '$page'
LEFT JOIN prop_intro ON prop_intro.id_prop = '$page'  
LEFT JOIN prop_hours ON prop_hours.id_prop = '$page'  
WHERE id_pg = '$page'"));

$check = mysql_query("SELECT type, GROUP_CONCAT(CONCAT('<tr><td>', feat, '</td></tr>') SEPARATOR '') AS featlist
FROM prop_feat
WHERE id_prop = '$page'
GROUP BY type");
4

2 回答 2

1

您需要使用GROUP BY子句,因为GROUP_CONCAT()它是一个聚合函数。

SELECT  Title, GROUP_CONCAT(FEAT) FeatList
FROM    Prop_Feat
GROUP   BY Title

输出

╔════════════╦═══════════════════╗
║   TITLE    ║     FEATLIST      ║
╠════════════╬═══════════════════╣
║ Appliances ║ Gas Range,Fridge  ║
║ Interior   ║ Hardwood Flooring ║
╚════════════╩═══════════════════╝
于 2013-04-01T12:10:36.633 回答
1

尝试:

SELECT p.*, f.title, group_concat(distinct f.feat) features
FROM properties p
left join prop_feat f on p.id_pg = f.id_prop
WHERE p.id_pg = '$page'
group by f.title
order by f.title
于 2013-04-01T12:29:17.633 回答