Welcome Guest, Not a member yet? Register   Sign In
searching with LIKE
#1

[eluser]Peter Dräxler[/eluser]
Hi guys.

PHP:
Code:
function like_find($keywords, $limit = 0, $offset = 0)
    {  
        $this->db->select('products.*, suppliers.Name AS Supplier');
        $this->db->from('products, suppliers');
        $this->db->where('products.Supplier = suppliers.Id');
        
        $this->db->like('products.Code', $keywords);
        $this->db->or_like('products.PartNo', $keywords);
        $this->db->or_like('products.EAN', $keywords);
        $this->db->or_like('products.Name', $keywords);
        $this->db->or_like('products.Manufacturer', $keywords);

        $this->db->order_by('products.Price', 'ASC');
        
        if($limit > 0)
        $this->db->limit($limit, $offset);
        
        return $this->db->get()->result('array'). exit($this->db->last_query());
    }

SQL:
Quote:SELECT `products`.*, `suppliers`.`Name` AS Supplier FROM (`products`, `suppliers`) WHERE `products`.`Supplier` = suppliers.Id AND `products`.`Code` LIKE '%text%' OR `products`.`PartNo` LIKE '%text%' OR `products`.`EAN` LIKE '%text%' OR `products`.`Name` LIKE '%text%' OR `products`.`Manufacturer` LIKE '%text%' ORDER BY `products`.`Price` ASC

I want 1 line of every suitable product but it get 9 lines of every product :-(

Please help!

Thank's
#2

[eluser]bretticus[/eluser]
You need a GROUP BY clause.
#3

[eluser]jmadsen[/eluser]
I think a DISTINCT might work better for him.

GROUP BY is typically used when you want to use aggregates. It's not a correct way of eliminating duplicates, unless your query forces you into it for some reason.
#4

[eluser]bretticus[/eluser]
True. I suppose it depends on how the tables are structured ultimately. Since we don't know...
#5

[eluser]Peter Dräxler[/eluser]
Thank's for reply.

Table products with sample data: http://dl.dropbox.com/u/1645785/products-table.png
Table suppliers with actually data: http://dl.dropbox.com/u/1645785/suppliers-table.png

I think, GROUP BY or DISTINC don't help me.

I can have 2 records which will have different just Supplier and I want to view both.




Theme © iAndrew 2016 - Forum software by © MyBB