Column 'size' in field list is ambiguous

297 Views Asked by At

Invalid SQL:

SELECT
 info_hash,
 size,
 comment,
 created_by,
 announce_list,
 completed_by, 
 completed,
 seeders,
 leechers,
 ulspeed,
 dlspeed,
 dateline,
 thumbnail_dateline, 
 filename,
 filesize,
 visible,
 attachmentid,
 counter,
 postid, 
 IF(thumbnail_filesize > 0, 1, 0) AS hasthumbnail,
 thumbnail_filesize,
 attachmenttype.thumbnail AS build_thumbnail,
 attachmenttype.newwindow
FROM attachment
LEFT JOIN attachmenttype AS attachmenttype USING (extension)
WHERE postid IN (-1,2)
ORDER BY attachmentid;

MySQL Error : Column 'size' in field list is ambiguous Error Number : 1052

2

There are 2 best solutions below

3
On

This means that size is presumably in both attachment and attachmenttype.

If you qualify your column names, then you won't ever have this type of problem.

0
On

@GordonLinoff has the right answer. But if you simply copied this code from somewhere, then you'll have difficulty understanding what he's saying. (Also asking nicely is better).

Use this as a basis. Notice how I added A. to size If any field is incorrect again, you'll have to add either A. or T. to it.

SELECT info_hash, 
 A.size,
 comment,
 created_by,
 announce_list,
 completed_by, 
 completed, 
 seeders, 
 leechers, 
 ulspeed, 
 dlspeed,
 dateline, 
 thumbnail_dateline, 
 filename, 
 filesize, 
 visible,
 attachmentid, 
 counter,
 postid,
 IF(thumbnail_filesize > 0, 1, 0) AS hasthumbnail,
 thumbnail_filesize,
 T.thumbnail AS build_thumbnail, 
 T.newwindow
FROM attachment A
LEFT JOIN attachmenttype AS T USING (extension)
WHERE A.postid IN (-1,2)
ORDER BY A.attachmentid;