0

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

项目技术:

project_tech_id  project_tech_name
        1            Mechanical
        2            Civil
        3            Computers

项目详细信息:

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_detai 表中的 project_tect_id=3 意味着我想要两个显示两列数据......如何使用一个查询来显示这个......实际上我正在使用这个查询,但我收到错误:

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

请帮助我...

4

7 回答 7

1

您应该使用 = 而不是 in

于 2013-06-06T10:43:03.527 回答
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 .")".);

于 2013-06-06T10:52:04.363 回答
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"
于 2013-06-06T10:52:31.400 回答
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}");
于 2013-06-06T10:46:04.407 回答
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子句

于 2013-06-06T10:47:09.723 回答
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);
于 2013-06-06T10:44:54.003 回答
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} ");
于 2013-06-06T10:48:20.973 回答