CodeIgniter删除条件和给定数组中的位置

时间:2017-08-03 10:10:56

标签: php mysql codeigniter activerecord query-builder

如何在Codeigniter中运行删除查询。我有一组数组和一个键。在MySQL中我的查询运行完美。

DELETE FROM `TABLE` WHERE `style_id`=2 && `ID` NOT IN (5,9,12)

在Codeigniter中如何编写和运行此查询。我尝试了以下代码,但没有工作。

$ids = '5,9,12';
$this->db->where('style_id', $style_id)->where_not_in('ID', $ids);
$this->db->delete('TABLE');

5 个答案:

答案 0 :(得分:4)

试试这个:你必须有$ ID作为数组

abstract class DocumentTypes {
    const OPERATIONAL = 1; 
}

// --------------

function handle($type) {
    if (!defined('DocumentTypes::' . $type)) {
        throw new Exception('Property value must exist');   
    }
}

// --------------

$data = 'asasfasfasfafs'; 
try {
    handle($data); 
} catch(Exception $e) {
    die($e); 
}

答案 1 :(得分:1)

如果你想这样,你可以用单行写这个:

    getLocation = (LocationManager) getSystemService(LOCATION_SERVICE);
    listenLocation = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            townName.append(location.getLatitude() + " " + location.getLongitude());



        }

        @Override
        public void onProviderDisabled(String provider) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);

        }
    };
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
           requestPermissions(new String[]{
                   android.Manifest.permission.ACCESS_FINE_LOCATION,android.Manifest.permission.ACCESS_COARSE_LOCATION,
                   android.Manifest.permission.INTERNET
           }, 10);
            return;
        }
    }else{
        configureButton();
    }



}

public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
    switch(requestCode){
        case 10:
            if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                configureButton();
            return;
    }
}

private void configureButton() {
    location.setOnClickListener(new View.OnClickListener(){
        public void onClick(View view){
            getLocation.requestLocationUpdates("gps", 5000, 0, listenLocation);
        }
    });
}

简短而简单。

答案 2 :(得分:0)

试试这个

this->db->where('style_id', $style_id);
this->db->where_not_in('ID', $ids);
$this->db->delete('TABLE');

这将使用AND子句

连接两个参数

数据应该是

<强>阵列

$ids = array(10, 20, '30);

<强>编号

$style_id = 15;

阅读Looking for Specific Data in codeigniter.com

答案 3 :(得分:0)

您可以直接运行:

DELETE FROM `TABLE` WHERE `style_id`=2 && `ID` NOT IN (5,9,12)
in $this->db->query("DELETE FROM `TABLE` WHERE `style_id`=2 && `ID` NOT IN (5,9,12)");

答案 4 :(得分:0)

//试一试

$ids = array(5,9,12);
$style_id =2;
$this->db->where('ID', $style_id);
$this->db->where_not_in('ID', $ids);
$this->db->delete('TABLE');