-2

大家好,我是 php 和 mysql 的初学者,我在这里加入了 3 个表,但我的查询没有运行,因为它不正确。thres 是其中的语法错误。请帮忙,我会很感激:))

$result = mysql_query("SELECT   courses.id AS cid,
                                courses.title,
                                courses.subjectsid,
                                subjects.id AS sid,
                                subjects.subjectname,
                                requestrecord.status
                       FROM     courses,
                                subjects,
                                requestrecord
                       LEFTJOIN requestrecord
                         ON     courses.id = requestrecord.coursesid
                       WHERE   (courses.subjectsid = subjects.id )");
4

2 回答 2

1

除了 LEFT JOIN 问题之外,您还要加入 requestrecord 表两次(一次作为隐式笛卡尔连接)。尝试:

$result = mysql_query("SELECT courses.id AS cid, courses.title, courses.subjectsid,  subjects.id AS sid, subjects.subjectname, requestrecord.status FROM courses JOIN subjects ON courses.subjectsid = subjects.id LEFT JOIN requestrecord ON courses.id = requestrecord.coursesid");
于 2012-10-29T18:53:22.277 回答
0

您有一个奇怪的隐式(WHERE 子句)和显式连接以及LEFT JOIN. 建议只选择显式连接。最终,您的方法会针对requestrecord.

SELECT
  courses.id AS cid, 
  courses.title, courses.subjectsid,  
  subjects.id AS sid, 
  subjects.subjectname, 
  requestrecord.status 
FROM 
  courses 
  /* Explicit inner join against `subjects` */
  JOIN subjects ON courses.subjectsid = subjects.id
  /* LEFT JOIN against requestrecord */
  LEFT JOIN requestrecord ON courses.id = requestrecord.coursesid
于 2012-10-29T18:53:45.133 回答