我想检查两个日期是否相等,$query_array['datepicker']
和absensi_tanggal
。它们具有不同的格式,因此我CONVERT
absensi_tanggal
。
SQL会自动转换日期吗?我试过不转换它,但查询不起作用,它总是返回0行。
所以我尝试了这段代码,但它没有用。
$q = $this->db->select('*', 'CONVERT(VARCHAR(10), absensi_tanggal, 101) AS temp')->from('msabsensi')->limit($limit, $offset)
->join('msumat', 'msabsensi.umat_id = msumat.umat_id')
->join('mskelas', 'mskelas.kelas_id = msumat.kelas_id');
$q->where('CONVERT(VARCHAR(10), absensi_tanggal, 101)', $query_array['datepicker']);
$result['rows'] = $q->get()->result();
这是我得到的错误(我认为我在CONVERT AS
中所做的select()
无法正常工作):
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'VARCHAR(10), absensi_tanggal, 101) '04/01/2013' LIMIT 10' at line 5
SELECT * FROM (`msabsensi`) JOIN `msumat` ON `msabsensi`.`umat_id` = `msumat`.`umat_id` JOIN `mskelas` ON `mskelas`.`kelas_id` = `msumat`.`kelas_id` WHERE CONVERT(VARCHAR(10), absensi_tanggal, 101) '04/01/2013' LIMIT 10
Filename: C:\xampp\htdocs\ci_gabdb\system\database\DB_driver.php
Line Number: 330
感谢任何帮助,谢谢:D
答案 0 :(得分:1)
问题是temp
不用作此查询中任何列的别名,而是在where子句中引用它
$this->db->select('*')
->from('msabsensi')->limit($limit, $offset)
->join('msumat', 'msabsensi.umat_id = msumat.umat_id')
->join('mskelas', 'mskelas.kelas_id = msumat.kelas_id')
//->where('temp = '.$query_array['datepicker']) // here
->where('absensi_tanggal = DATE_FORMAT( STR_TO_DATE( "' .$query_array['datepicker'] . '", \'%m/%d/%Y\' ) , \'%Y-%m-%d\' )')
->get()->num_rows();
修改强>
Using column alias in WHERE clause of MySQL query produces an error