1

I have two tables firm and contactdetails. I am trying to get the the firm name from firm and certain contact details from contactdetails. I am using $id =$_GET['id']; to get the id . In contactdetails i have fk_firm_id which is my foreign key. I am not sure how to use the inner join query. I am trying the following query:

$sql="SELECT  firm.`name` ,`address_physical_line_1` , `fax_1` , `phone_1` 
FROM contactdetails JOIN firm ON contactdetails.fk_firm_id='$id'";
echo $sql;
$result = mysql_query($sql);

but i am not getting the correct firm. Can anyone help me with this query, please.

4
  • you need to specify a column on join condition,not a value.
    – Mihai
    Aug 12, 2013 at 8:27
  • You can not join var. You have to define table columns. Read Document
    – Bora
    Aug 12, 2013 at 8:27
  • You wouldn't join using var. This would just be a where Aug 12, 2013 at 8:28
  • And once again: mysql_* is deprecated, don't use it. Also don't trust user input, you have to check every variable provided by user. It's good idea to use prepared statements with user data.
    – Elon Than
    Aug 12, 2013 at 8:39

6 Answers 6

1

You should use like this JOIN firm ON contactdetails.fk_firm_id = firm.id

$sql=" SELECT  firm.`name` ,`address_physical_line_1` , `fax_1` , `phone_1` 
       FROM    contactdetails 
       JOIN    firm ON contactdetails.fk_firm_id = firm.id
       WHERE   contactdetails.fk_firm_id = '$id'
     ";

$result = mysql_query($sql);
1

This is assuming that your firm table has a primary key called id

 $sql="SELECT  firm.`name` ,`address_physical_line_1` , `fax_1` , `phone_1` FROM contactdetails JOIN firm ON `contactdetails`.`fk_firm_id`=`firm`.`id`
    WHERE `firm`.`id` = '$id'";
    echo $sql;
    $result = mysql_query($sql);
1

There is a mistake about JOIN and WHERE statements:

$sql = "SELECT 
              f.name,
              c.address_physical_line_1, 
              c.fax_1, 
              c.phone_1 
        FROM 
              contactdetails c JOIN firm f ON c.fk_firm_id= f.id 
        WHERE c.id = '$id'";
1
$sql="SELECT  firm.`name` ,`address_physical_line_1` , `fax_1` , `phone_1` FROM
contactdetails JOIN firm ON contactdetails.fk_firm_id=firm.id where 
contactdetails.fk_firm_id='$id'";

you should join on a firm's field such as firm.id

syntax: FROM table1 LEFT JOIN table2 ON table1.field1 compopr table2.field2 compopr is : "=","<",">","<=",">=","<>"

1

You are missing the WHERE clause that limits the result set to only the firm you're interested in; now you're getting all firms joined with a single contact details record.

.. where firm.id=$id

For new applications, please use a database API that has prepared statements, like mysqli or pdo.

2
  • You missed to point out the main mistake in the question wrong use of join Aug 12, 2013 at 8:43
  • 1
    @dianuj, nope, that's not an issue. The way the OP wrote the join is unusual, but it's not wrong. Thanks for correcting the typo though, editing text on a mobile is such a PIA.
    – Joni
    Aug 12, 2013 at 8:44
0

Use the following query for inner join

$sql="SELECT  firm.name ,address_physical_line_1 , fax_1 , phone_1 FROM 
contactctdetails  INNER JOIN firm ON contactdetails.fk_firm_id=$id"; 

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.