此COUNT MySQL语句中的未知列错误?

时间:2013-10-21 18:46:27

标签: mysql sql

错误是

  

'where'子句中的未知列'num'

SELECT COUNT(*) AS num, books_bookid
FROM bookgenre_has_books
WHERE num > 10
GROUP BY books_bookid

我做错了什么?感谢。

4 个答案:

答案 0 :(得分:7)

WHERE子句无法查看别名,请使用HAVING

不允许在WHERE子句中引用列别名,因为在执行WHERE子句时可能尚未确定列值

http://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.html

答案 1 :(得分:3)

试试这个,你应该使用HAVING子句

SELECT COUNT(*) AS num, books_bookid
FROM bookgenre_has_books
GROUP BY books_bookid
HAVING COUNT(*) > 10

SQL HAVING子句与SQL GROUP BY子句结合使用。它可以在SQL SELECT语句中用于过滤SQL GROUP BY返回的记录。

答案 2 :(得分:1)

我们可以这样写

    SELECT COUNT(*) , books_bookid
   FROM bookgenre_has_books
   GROUP BY books_bookid
   having count(*) > 10

您正在检查该列的重复项 books_bookid

答案 3 :(得分:1)

试试这个

var ss = SpreadsheetApp.getActiveSpreadsheet(); 
var sheet = ss.getActiveSheet();

var email = "xxx@gmail.com";      
var timezone = ss.getSpreadsheetTimeZone();

var today     = new Date();
var oneDayAgo = new Date(today.getTime() - 1 * 24 * 60 * 60 * 1000);  
var startTime = oneDayAgo.toISOString();

var search = '(trashed = false or trashed = false) and (modifiedDate > "' +    startTime + '")';
var folder1 = DriveApp.getFoldersByName('SaveToPDF').next();
var files1  = folder1.searchFiles(search);

var row = "", count=0;

while( files1.hasNext() ) {

var file1 = files1.next();

var fileName = file1.getName();
var fileURL  = file1.getUrl();
var lastUpdated =  Utilities.formatDate(file1.getLastUpdated(), timezone, "yyyy-MM-dd HH:mm");
var dateCreated =  Utilities.formatDate(file1.getDateCreated(), timezone, "yyyy-MM-dd HH:mm")

row += "<li>" + lastUpdated + " <a href='" + fileURL + "'>" + fileName + "</a></li>";

sheet.appendRow([dateCreated, lastUpdated, fileName, fileURL]);

count++;
}
if (row !== "") {
row = "<p>" + count + " file(s) have changed in your Google Drive in the past 24 hours. Here's the list:</p><ol>" + row + "</ol>";
row +=  "<br><small>To stop these notifications, please <a href='" + ss.getUrl() + "'>click here</a> and choose <em>Uninstall</em> from the Drive Activity menu.<br/></small>";
MailApp.sendEmail(email, "Google Drive - File Activity Report", "", {htmlBody: row, cc: "xxx@gmail.com"} );
  }