使用id从两个表中获取数据

时间:2013-06-06 10:40:26

标签: php

我想使用onclick Id显示两个表中的元素。 这些是我的表格:

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

使用project_tech_id在两个表之间存在关系。使用表单将值插入到project_detail中。 现在我想显示标题,内容,project_tech_name其中project_tect_id = 3来自project_detai表意味着我想要两个显示两列数据... 如何使用一个查询显示它 实际上我使用这个查询,但我收到错误:

$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);

请帮帮我......

7 个答案:

答案 0 :(得分:1)

您应该使用=而不是

答案 1 :(得分:0)

尝试执行它

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);

答案 2 :(得分:0)

使用 JOIN ,您可以这样做:

$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}");

答案 3 :(得分:0)

$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.")");

pt.project_tech_Id = " .$prj_id);

//我刚刚删除了From子句之前的逗号

答案 4 :(得分:0)

你的查询可以是:这是一个优化的,因为它使用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} ");

答案 5 :(得分:0)

在您的SQL查询中,有一个字段名称“pd.video_url”,它不在您的表结构中,并且在此字段值之后还有一个额外的逗号(,)。

第二个问题是,使用“=”而不是“in”

如果有多个“project_tech_Id”,则可以使用“in”之类的

$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 .")".);

$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 .")".);

答案 6 :(得分:0)

试试这个:

"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"