如何使用多个值运行delete sql query

时间:2016-03-05 17:09:25

标签: php mysql

我想在其中运行带有多个值的delete sql查询。但我无法这样做。所以需要你的帮助。

在controller.php页面中:

function deleteFiles()
{
$mainframe = &JFactory::getApplication();
if ($this->_model->deleteFiles()) {
$mainframe->enqueueMessage('Files deleted successfully');
} else {
$mainframe->enqueueMessage('Files not deleted');
}
    ARequest::redirectList($this->_controllerName);
}

在model.php页面中:

function deleteFiles($field, $value)
{
$query = 'DELETE FROM ' . $this->rquote($this->_table->getTableName()) . ' WHERE ' . $this->rquote($field) . ' = ' . $this->_db->Quote($value);
$this->_db->setQuery($query);
return $this->_db->query();
}

在file.php页面

function deleteFiles()
{
$this->_db->setQuery('DELETE r 
            FROM #__booking_reservation AS r 
            WHERE r.state = ' . FILES_ACCEPT);
$this->_db->query();
return true;
}

现在在file.php页面中,我想添加FILES_BOOKED,FILES_CANCEL,FILES_DELETE以及当前的FILES_ACCEPT。但我不知道该怎么做。

你能帮我解决吗?

2 个答案:

答案 0 :(得分:0)

您可以在SQL下的WHERE查询中简单地定义OR每个案例DELETE r FROM #__booking_reservation AS r WHERE r.state = .. OR r.state = .. OR ...

$this->_db->setQuery('DELETE r 
        FROM #__booking_reservation AS r 
        WHERE r.state=' .FILES_ACCEPT. ' OR r.state=' .FILES_BOOKED);

<强>更新

class SomeClass
{
    protected $object1;
    protected $object2;

    public function __construct(
        Object1Interface $object1,
        Object2Interface $object2
    ) {
        $this->object1 = $object1;
        $this->object2 = $object2;
    }

// methods that use Object1 and Object2 classes by $this->object1 and $this->object2.
}

答案 1 :(得分:0)

这是你想要的吗?

. . . 
WHERE r.state IN (' . FILES_ACCEPT . ', ' . FILES_BOOKED . ', ' FILES_CANCEL . ', ' . FILES_DELETE . ')' );

这假设全大写字母名称包含state的有效值。