Welcome Guest, Not a member yet? Register   Sign In
Using Group BY - mysql
#1

[eluser]theknight[/eluser]
Code:
//SQL to get child ID
        $this->db->select("SupplyRequirements.Id, DateParentEmailed, SupplyItem_Id, Child_Id");
        //joins the two tables
        $this->db->join('SupplyRequirements', 'SupplyRequirements.SupplyItem_Id=SupplyItems.Id');
        $this->db->group_by("Child_Id");
        $query = $this->db->get_where("SupplyItems",'DateParentEmailed=Null');  
          $details = $query->result_array();

I get the following error:

A Database Error Occurred
Error Number:

Column 'SupplyRequirements.Id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

SELECT SupplyRequirements.Id, DateParentEmailed, SupplyItem_Id, Child_Id FROM SupplyItems JOIN SupplyRequirements ON SupplyRequirements.SupplyItem_Id=SupplyItems.Id WHERE DateParentEmailed=Null GROUP BY Child_Id

Cheers/
#2

[eluser]theknight[/eluser]
[quote author="theknight" date="1311361638"]
Code:
//SQL to get child ID
        $this->db->select("SupplyRequirements.Id, DateParentEmailed, SupplyItem_Id, Child_Id");
        //joins the two tables
        $this->db->join('SupplyRequirements', 'SupplyRequirements.SupplyItem_Id=SupplyItems.Id');
        $this->db->group_by("Child_Id");
        $query = $this->db->get_where("SupplyItems",'DateParentEmailed=Null');  
          $details = $query->result_array();

I get the following error:

A Database Error Occurred
Error Number:

Column 'SupplyRequirements.Id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

SELECT SupplyRequirements.Id, DateParentEmailed, SupplyItem_Id, Child_Id FROM SupplyItems JOIN SupplyRequirements ON SupplyRequirements.SupplyItem_Id=SupplyItems.Id WHERE DateParentEmailed=Null GROUP BY Child_Id

Cheers/[/quote]

Nevermind this is not what I am looking for, basically I have written a SQL query that returns the foreign key child_id

4
4
3
3
1

There are duplicate foreign keys 4,4,3,3. Now I want it to return the child_id without duplicates like this:

4
3
1

How could I do this?
#3

[eluser]toopay[/eluser]
add DISTINCT in whatsoever field in select statement.




Theme © iAndrew 2016 - Forum software by © MyBB