Php MYSQL选择没有指定值的数据
我想从包含排除字段列表的表中选择一些数据Php MYSQL选择没有指定值的数据,php,mysql,codeigniter,Php,Mysql,Codeigniter,我想从包含排除字段列表的表中选择一些数据 public function get_columns($table, $excluded_columns = 'id, deleted') { $this->db->select('column_name'); $this->db->from("information_schema.COLUMNS"); $this->db->where('TABLE_SCHEMA', $this->db
public function get_columns($table, $excluded_columns = 'id, deleted')
{
$this->db->select('column_name');
$this->db->from("information_schema.COLUMNS");
$this->db->where('TABLE_SCHEMA', $this->db->database);
$this->db->where('TABLE_NAME', $table);
$this->db->where('COLUMN_KEY !=', 'MUL');
$result = $this->db->get()->{$this->_return_type(1)}();
$result = array_column($result, 'column_name');
return $result;
}
在我的例子中,我想删除所有没有id的列名称,那么我如何选择没有指定值的数据呢?我不能告诉您这是否正确。但我可以在core中这样做:
/* Get the data into a temp table */
SELECT * INTO #TempTable
FROM YourTable
/* Drop the cloumns that are not needed */
ALTER TABLE #TempTable
DROP COLUMN ColumnToDrop
/* Get results and drop temp table */
SELECT * FROM #TempTable
DROP TABLE #TempTable
因此,请让我知道您的想法如果我理解正确,您需要做的就是将您不希望看到结果的列名添加到您的查询中,如下所示
public function get_columns($table, $excluded_columns = 'id, deleted')
{
$this->db->select('column_name');
$this->db->from("information_schema.COLUMNS");
$this->db->where('TABLE_SCHEMA', $this->db->database);
$this->db->where('TABLE_NAME', $table);
$this->db->where('COLUMN_KEY !=', 'MUL');
// 2 new criteria added
$this->db->where('COLUMN_NAME !=', 'id');
$this->db->where('COLUMN_NAME !=', 'deleted');
$result = $this->db->get()->{$this->_return_type(1)}();
$result = array_column($result, 'column_name');
return $result;
}
如果此函数只用于一个表,则可以生成现有字段的数组:
$fields = ['id', 'deleted', 'xxx', 'yyy', 'zzzz']
并将其赋给数组等排除列的函数
$excluded_columns = ['id', 'deleted'].
要获取要使用的字段,请执行以下操作:
$tmp_fields = array_diff[$fields, $excluded_fields]
在查询中,选择解析
$tmp\u字段$this->db->where('column\u name!=','id')代码>?您的意思是说您有两列,一列名为id
,另一列名为deleted
,但您不希望这些列出现在结果中?是的。我不希望它们出现在结果中。他不想删除这些列,他只想从查询结果中删除行。这只是一个查询,不是一个表维护问题