2

我试图在 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 查询呢?有任何想法吗?

4

1 回答 1

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();
于 2012-12-17T05:11:26.650 回答