user2330772
user2330772

Reputation: 65

Get the data from two tables using id

I want to display the elements from two tables using onclick Id. These are my tables:

Project_tech:

project_tech_id  project_tech_name
        1            Mechanical
        2            Civil
        3            Computers

project_detail:

id      title       content       project_tech_id
 1     sample        abcd              3
 2     sample2       efgh              1
 3     sample3       xyz               3

there is a relation between two tables using project_tech_id.The values are inserting into project_detail using form. now i want to display the title,content,project_tech_name where project_tect_id=3 from project_detai table means i want two display two columns data... how to display this using one query.. Actually iam using this query but i am getting error:

$prj_id = $_GET['id'];

$sql = mysql_query("SELECT pt.project_tech_name,
    pd.title, 
    pd.content,
    pd.video_url,
    FROM
      project_details pd,
      project_tech pt
      WHERE
      pt.project_tech_Id = pd.project_tech_Id AND
      pt.project_tech_Id in" .$prj_id);

pls...help me...

Upvotes: 0

Views: 2589

Answers (7)

Kiren S
Kiren S

Reputation: 3097

try this:

"SELECT 
project_tech.project_tech_name,
project_detail.title, 
project_detail.content,
project_detail.video_url

FROM project_tech
JOIN project_details
ON 
project_tech.project_tech_Id = project_details.project_tech_Id 
AND
project_tech.project_tech_Id = $prj_id"

Upvotes: 0

Rajiv Ranjan
Rajiv Ranjan

Reputation: 1869

In your SQL query there is field name "pd.video_url", which is not in your table structure and one extra comma(,) after this field value.

2nd issue is, use "=" instead of "in"

If there is multiple "project_tech_Id", then you can use "in" like

$prj_id = (1,4,7,2);

$sql = mysql_query("SELECT pt.project_tech_name,
    pd.title, 
    pd.content,
    pd.video_url,
    FROM
      project_details pd,
      project_tech pt
      WHERE
      pt.project_tech_Id = pd.project_tech_Id AND
      pt.project_tech_Id in(" .$prj_id .")".);

Upvotes: 0

user1578358
user1578358

Reputation: 26

try to execute it

mysql_query("SELECT pt.project_tech_name,pd.title,pd.content,pd.video_url
    FROM
      project_details pd Left Join project_tech pt
      ON
      pt.project_tech_Id = pd.project_tech_Id AND
      pt.project_tech_Id = " .$prj_id);

Upvotes: 0

jospratik
jospratik

Reputation: 1674

your query can be : this is a optimized on as it uses LEFT JOIN.

$sql = mysql_query("SELECT pt.project_tech_name,
    pd.title, 
    pd.content,
    pd.video_url,
    FROM project_details pd
     LEFT JOIN project_tech pt 
      ON pt.project_tech_Id = pd.project_tech_Id
    WHERE  pt.project_tech_Id = {$prj_id} ");

Upvotes: 0

Bere
Bere

Reputation: 1747

$sql = mysql_query("SELECT pt.project_tech_name,
    pd.title, 
    pd.content,
    pd.video_url
    FROM
      project_details pd,
      project_tech pt
      WHERE
      pt.project_tech_Id = pd.project_tech_Id 
      AND
      pt.project_tech_Id in (" .$prj_id.")");

or

pt.project_tech_Id = " .$prj_id);

// I just removed comma before From clause also

Upvotes: 0

letiagoalves
letiagoalves

Reputation: 11302

Using JOIN you can do it like this:

$prj_id = $_GET['id'];

$sql = mysql_query("SELECT pt.project_tech_name,
            pd.title, pd.content, pd.video_url
            FROM project_details pd JOIN project_tech pt
            ON pd.project_tech_id = pt.project_tech_id
            WHERE pt.project_tech_Id = {$prj_id}");

Upvotes: 0

mrida
mrida

Reputation: 1157

You should use = instead of in

Upvotes: 1

Related Questions