0

Im having a problem with a sql query. This is the original function:

function selectAllSorted($active)
{
    return $this->selectObjects("SELECT bp.*, p.title as product_title
                                 FROM $this->_table bp 
                                 INNER JOIN ?_product p USING (product_id)
                                 ORDER BY 0+sort_order,p.title");
}

Which i modified it to look like this:

function selectAllSorted($active)
{
    return $this->selectObjects("SELECT bp.*, p.title as product_title
                                 FROM $this->_table bp
                                 WHERE product_id =$active
                                 INNER JOIN ?_product p USING (product_id)
                                 ORDER BY 0+sort_order,p.title");
}

But i get an unknown error and since i cant see the logs i cant pinpoint where it is or why is it wrong to use WHERE here.

This is how the function selectObjects is defined:

function selectObjects($sql, $param1 = null)
    {
        $args = func_get_args();
        $q = call_user_func_array(array($this->_db, 'queryResultOnly'), $args);
        $ret = array();
        while ($row = $this->_db->fetchRow($q))
        {
            $obj = new $this->_recordClass($this);
            $obj->fromRow($row);
            $ret[] = $obj;
        }
        return $ret;
    }

Can you guys figure out whats wrong?

Thank you.

1
  • Move the WHERE clause and place it after the JOIN Commented Apr 7, 2014 at 5:44

2 Answers 2

3

Your syntax is wrong - the where clause comes after the join clause(s):

function selectAllSorted($active)
{
    return $this->selectObjects("SELECT bp.*, p.title as product_title
                                 FROM $this->_table bp
                                 INNER JOIN ?_product p USING (product_id)
                                 WHERE bp.product_id =$active
                                 ORDER BY 0+sort_order,p.title");
}
Sign up to request clarification or add additional context in comments.

3 Comments

It will probably be necessary to select product_id from one of the tables, i.e. WHERE p.productid =..., won't it?
@TomasPastircak yup, good catch! Added the bp prefix to my answer.
Thank you! That was fast! I though the order didnt matter.
0

Join Query syntex is:

Select t1.* from table1 t1
inner join table2 t2  on t1.col1=t2.col1
where t1.col2> 0 and t2.col3>4

Now, your query look like :

function selectAllSorted($active)
{
    return $this->selectObjects("SELECT bp.*, p.title as product_title
                                 FROM $this->_table bp
                                 INNER JOIN ?_product p on pb.product_id=p.product_id
                                 WHERE bp.product_id =$active                                
                                 ORDER BY 0+sort_order,p.title");
}

1 Comment

Thank you, that was useful too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.