【问题标题】:SELECT multiple rows and concatenate into one - in joomla选择多行并连接成一个 - 在joomla中
【发布时间】:2015-08-14 22:12:50
【问题描述】:

我在选择多行并将它们作为一行检索时遇到问题。问题是 Joomla 将用户配置文件中的数据存储在名为 profile_key 和 profile_value 的两列中。我想从名为“profile.li_vid”的“profile_key”列中检索与值为“1”的“profile_value”列对应的“profile.li_kre”列中的所有行。

数据库草稿

user_id | profile_key        | profile_value
475     | profile.li_vid     | "1"
475     | profile.li_kre     | "1"
475     | profile.address1   | "1"
476     | profile.li_vid     | "1"
476     | profile.li_kre     | "1"
476     | profile.address1   | "1"

代码

  $db = JFactory::getDbo(); 
    $query = $db->getQuery(true);
    $query
        ->select ($db->quoteName(array('a.id','a.name', 'b.profile_value','b.profile_key')))
        ->from ($db->quoteName('#__users','a'))
        ->where ($db->quoteName('b.profile_key') . " = " . $db->quote('profile.li%'))

        // ->where ($db->quote('profile_value') . " = \"1\" " )
        ->join('INNER', $db->quoteName('#__user_profiles','b').'ON('.$db->quoteName('a.id').'='.$db->quoteName('b.user_id').')')
        ->order($db->quoteName('a.name').'ASC')
     ;
    $db->setQuery($query);
    $rows = $db->loadObjectList();

当我在 foreach 循环中将它们回显到屏幕时,它们会出现在我表中的单独行中,因为代码逐行遍历每个查询结果。我想到了一个解决方案,将多行连接成一个 - 但是如何? 我希望你能帮助我。

【问题讨论】:

  • 你可以使用 mysqls GROUP_CONCAT 不过我不知道如何在 Joomla 中使用。
  • 是的 - 我试过了,但是当我把它放在我的 SELECT 行时收到一个错误 - L
  • 你能用纯 SQL 发布你的查询吗?那我就可以帮你了。
  • 感谢 Daan - 是的,在纯 SQL 中是可能的 - 但在 Joomla 中我遇到了麻烦!不过谢谢你的回复。

标签: php mysql joomla


【解决方案1】:

记住:GROUP_CONCAT() 默认有 1024 个字符限制!

【讨论】:

    猜你喜欢
    • 2012-01-01
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-20
    • 2015-04-19
    相关资源
    最近更新 更多