如何在joomla中编写此查询

时间:2015-02-17 09:29:45

标签: php mysql joomla

我想获得子类别的所有父类别标题 所以在mysql中我可以使用此代码

执行此操作
SELECT T2.id, T2.title
FROM (
    SELECT
        @r AS _id,
        (SELECT @r := parent_id FROM `up8te_categories` WHERE id = _id) AS parent_id,
        @l := @l + 1 AS lvl
    FROM
        (SELECT @r := 8, @l := 0) vars,
        `up8te_categories` h
    WHERE @r <> 0) T1
JOIN `up8te_categories` T2
ON T1._id = T2.id
ORDER BY T1.lvl DESC

但是在joomla中我们有API用于查询 我想知道如何在joomla中使用它?

我知道这样的事情:

$db = JFactory::getDbo();
$query = $db->getQuery(true);

$query->select($db->quoteName(array('title')))
      ->from(......??????......)
      ->join('INNER', $db->quoteName('#__categories', 'T2') . ' ON (T1._id  =  T2.id)')
      ->order('T1.lvl DESC');
$db->setQuery($query);
$rows = $db->loadObjectList();

但我不知道如何设置从我想再次选择..

我能做什么?

亲切的问候。

1 个答案:

答案 0 :(得分:0)

我更喜欢将$ query用作字符串,如下所示:

    $db = JFactory::getDbo();        
    $query = 'SELECT T2.id, T2.title
             FROM (
                 SELECT
                     @r AS _id,
                     (SELECT @r := parent_id FROM #__categories WHERE id = _id) AS parent_id,
                     @l := @l + 1 AS lvl
                 FROM
                     (SELECT @r := 8, @l := 0) vars,
                     #__categories h
                 WHERE @r <> 0) T1
             JOIN #__categories T2
             ON T1._id = T2.id
             ORDER BY T1.lvl DESC';

    $db->setQuery($query);