JOIN查询语法错误

时间:2012-10-29 18:48:25

标签: php mysql

大家好我是初学者在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 )");

2 个答案:

答案 0 :(得分: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");

答案 1 :(得分: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