Joomla 2.5使用LEFT JOIN删除查询

时间:2012-12-16 22:19:59

标签: joomla joomla2.5

我试图在Joomla 2.5中使用LEFT JOIN实现删除代码:

$cid = JRequest::getVar('cid', array(), 'post', 'array');

$query = $db->getQuery(true);

$query->delete($db->quoteName('table1').' AS t1');

$query->leftJoin($db->quoteName('table2').
' AS t2 ON t2.table_1_id = t1.id ');

$query->where(array('t1.id IN ('.  implode(',', $cid).')'));

$db->setQuery($query);

try {
  $db->query(); 
} catch (Exception $e) {
  echo $e->getMessage();
}

我得到的是:

DELETE FROM `table1` AS t1
LEFT JOIN `table2` AS t2 ON t2.table_1_id = t1.id 
WHERE t1.id IN (48)

此SQL查询不正确。我需要的是:

DELETE t1.*,t2.* FROM `table1` AS t1
LEFT JOIN `table2` AS t2 ON t2.table_1_id = t1.id 
WHERE t1.id IN (48)

那么我应该如何更改Joomla查询以获得正确的SQL查询?有任何想法吗?

1 个答案:

答案 0 :(得分:3)

我和Joomla已经有很长一段时间了。根据我的Joomla知识,你无法完成你想要做的事$query->delete()。由于您的查询有点棘手,因此可以使用以下方法执行。

$db = JFactory::getDBO();
$query = "DELETE t1.*,t2.* FROM `table1` AS t1
LEFT JOIN `table2` AS t2 ON t2.table_1_id = t1.id 
WHERE t1.id IN (48)"; // you can replace the line with array('t1.id IN ('.  implode(',', $cid).')')
$db->setQuery($query);
$db->query();