I want to convert a normal mysql select query to codeigniter
SELECT count(*) leave_status, teacher_id FROM teacher_attendance WHERE teacher_id='1' & leave_status='1' group by teacher_id
this is what i have tried
$attendance = $this->db->get_where('teacher_attendance', array('year' => $running_year, 'timestamp' => $timestamp, 'teacher_id' => $row['teacher_id']))->result_array();
what i want to do is get the count of leave_status by teacher id
$this->db->select("count(*) as leave_status, teacher_id");
$this->db->from("teacher_attendance");
$this->db->where("teacher_id = 1 AND leave_status='1'");
$this->db->group_by("teacher_id");
$query = $this->db->get();
if ($query->num_rows() > 0) {
$result = $query->result_array();
}
Related
I have 2 table
Donasi and sistem_list_Date.
This is my query
$this->db->SELECT("sistem_list_tanggal.tanggal, COUNT(id) AS jumlah")
->FROM('sistem_list_tanggal')
->JOIN('donasi','donasi.tanggal_ambil=sistem_list_tanggal.tanggal','LEFT')
->WHERE('sistem_list_tanggal.tanggal >=', '2019-12-01')
->WHERE('sistem_list_tanggal.tanggal <=', '2019-12-31')
->WHERE('status',1)
->GROUP_BY('sistem_list_tanggal.tanggal');
$query = $this->db->get();
$output = array('data' => array());
if($query->num_rows() > 0){
return $query->result_array();
}else{
return array();
}
Why this query returning only date with value (jumlah).
I want query return all dates 2019-12-01 until 2019-12-31, if null data return 0
I have followed the tutorial here https://stackoverflow.com/a/14336188/11540418
YOU have to use left join for the same query
I'm trying to rewrite an SQL select query to use the $wpdb->prepare function. My original SQL query looks like this and works fine.
function hfwp_SaveFormData($formData, $userID, $today, $dbTable) {
global $wpdb;
$wpdb->show_errors();
$query = "SELECT UserID FROM $dbTable WHERE Date = '$today' AND UserID = '$userID'";
$wpdb->get_results( $query );
$count = $wpdb->num_rows;
if ($count > 0) {
$wpdb->update($dbTable, array('Distance' => $formData), array('Date' => $today, 'UserID' => $userID));
}else{
$wpdb->insert($dbTable, array('UserID' => $userID, 'Date' => $today, 'Distance' => $formData));
}
}
I then tried to rewrite the SELECT statement to use $wpdb->prepare:
$wpdb->get_results( $wpdb->prepare("SELECT UserID FROM %s WHERE Date = %s AND UserID = %d", $dbTable, $today, $userID ) );
But it gives an error in the log but not enough to give me a hint on what is wrong.
I also tried another version of the $wpdb->prepare:
$query = "SELECT UserID FROM %s WHERE Date = %s AND UserID = %d";
$prepared = $wpdb->prepare( $query , $dbTable, $today, $userID );
$wpdb->get_results( $prepared );
I am very confused about this (returning false):
$sql = "SELECT * from tbl_user WHERE group = 'abc'";
$res = mysql_query($sql);
if(mysql_num_rows($res) > 0) {
$response = array('status' => '1');
} else {
$response = array('status' => '0'); // ---> what I get back
die("Query failed");
}
...despite the fact the field group is present in mySQL database. Even more strange is that the following return the value of group:
$SQL = "SELECT * FROM tbl_user";
$result = mysql_query($SQL);
while ($db_field = mysql_fetch_assoc($result)) {
print $db_field['group']; // ---> returns 'abc'
When I execute a WHERE clause with every other fields of my table excepting group (for example WHERE name = 'ex1' AND ID=1 AND isAllowed=0 (and so on...), everything is fine. As soon as I insert group = 'abc', I get nothing...
This makes me mad. If anyone could help... (I am running a local server with MAMP).
Thanks a lot!
The issue is that group is a reserved word in SQL.
For MySql you need to escape it with backticks
`group`
So your query would be
$sql = "SELECT * from tbl_user WHERE `group` = 'abc'";
I want to run following query in symfony doctrine.
SELECT p.id AS id FROM skiChaletPrice p WHERE ski_chalet_id = ? AND month = ?
I wrote my doctrine query as following.
$q = Doctrine_Query::create()
->select('p.id AS id')
->from('skiChaletPrice p')
->andWhere('ski_chalet_id = ?', $chaletId)
->andWhere('month = ?', $from);
$result = $q->fetchOne();
if ($result->count() > 0) {
return $result->toArray();
} else {
return null;
}
But my result always include all columns in the table. What the issue? Please help me.
The issue is that fetchOne() will return a Doctrine object, which implicitly contains all the columns in the table. $result->toArray() is converting that doctrine object to an array, which is why you get all the columns.
If you only want a subset of column, don't hydrate an object, instead do something like this:
$q = Doctrine_Query::create()
->select('p.id AS id')
->from('skiChaletPrice p')
->andWhere('ski_chalet_id = ?', $chaletId)
->andWhere('month = ?', $from);
$results = $q->execute(array(), Doctrine::HYDRATE_SCALAR);
See http://docs.doctrine-project.org/projects/doctrine1/en/latest/en/manual/data-hydrators.html
This is how I should do it:
$result = Doctrine_Query::create()
->select('id')
->from('skiChaletPrice')
->andWhere('ski_chalet_id = ?', $chaletId)
->andWhere('month = ?', $from)
->limit(1)
->fetchOne(array(), Doctrine_Core::HYDRATE_SINGLE_SCALAR);
// result will be a single id or 0
return $result ?: 0;
// if you want array($id) or array() inseatd
// return (array) $result;
SELECT
case_id,
serialnumber,
DateLastHere,
Bookedindate,
DATEDIFF(DateLastHere,Bookedindate)
FROM `cases`
WHERE serialnumber like 'TTTT46'
You can do it like this
$data = array(
'case_id',
'serialnumber',
'DateLastHere',
'Bookedindate',
'DATEDIFF(DateLastHere,Bookedindate)'
);
$this->db->select($data);
$this->db->from('cases');
$this->db->like('serialnumber','TTTT46');
$query = $this->db->get()->result_array();