Welcome Guest, Not a member yet? Register   Sign In
Active Records / Query Help - How to use db->where [SOLVED]
#1

[eluser]snowstar[/eluser]
Hi All,

I'm having trouble writing a query. It currently looks like this:

Code:
$this->db->select('id, date, clientid');
        $this->db->where('date', $date);
        $query = $this->db->get('tbl_sales');
        return $query->result();

I need the query to return data from a table where date matches my $date and where clientid is unique (its possible for the clientid to have a clients id appear more than once)

Any help is greatly appreciated
#2

[eluser]snowstar[/eluser]
Ive tried this but didn't work for me :

Code:
$query = $this->db->query("SELECT DISTINCT clientid FROM mytable where date=$date");
return $query->result();

Not sure if im using the where correctly or if i can use where in the query
#3

[eluser]snowstar[/eluser]
SOLUTION

Code:
function get_total_new_customers($date)
    {
$query = $this->db->query("SELECT DISTINCT clientid FROM mytable WHERE date = '$date'");    
        return $query->result();
    }

function get_total_existing_customers($date)
    {
        $query = $this->db->query("SELECT clientid, COUNT(*) as count
FROM mytable
WHERE date = '$date'
GROUP BY clientid
HAVING count > 1");    
        return $query->result();
    }

get_total_new_customers checks the db for new client id's where get_total_existing_customers counts for multiple client id's




Theme © iAndrew 2016 - Forum software by © MyBB