选择最后一行与内部联接

时间:2017-01-31 19:17:50

标签: php mysql select join

我有两个关系表[category]和[briefs]。 我想选择最后的ROW FROM简报WHERE briefs.category_id = category.category_id;

"Column 'category_id' in field list is ambiguous"

我收到以下错误:PIL.ImageGrab

3 个答案:

答案 0 :(得分:1)

即使两个类别都具有相同的值。你需要指定你所引用的表,因为它们都有一个具有该名称的列

同样使用explict JOIN sintaxis,Aaron Bertrand撰写了一篇很好的文章Bad habits to kick : using old-style JOINs

 SELECT briefs.brief_file_num,
        briefs.category_id    
 FROM briefs
 INNER JOIN category 
    ON briefs.category_id = category.category_id 
 ORDER BY briefs.brief_file_num 
 DESC LIMIT 1"

答案 1 :(得分:0)

两个表都有category_id,这是不明确的。请改用category.category_id

SELECT brief_file_num,category.category_id FROM briefs,category 
WHERE briefs.category_id = category.category_id ORDER BY briefs.brief_file_num 
DESC LIMIT 1

答案 2 :(得分:0)

您应该明确添加表名以避免歧义

   "SELECT briefs.brief_file_num, briefs.category_id 
     FROM briefs,category 
     WHERE briefs.category_id = category.category_id ORDER BY briefs.brief_file_num 
    DESC LIMIT 1";