简体   繁体   English

关于SQL View,我有些困惑

[英]About SQL View, I have something confused

I have these tables: 我有这些表:

  • BRANCH (Bno, Street, Area, City, Pcode, Tel_No, Fax_No) BRANCH (Bno,街道,区域,城市,Pcode,电话号码,传真号码)
  • STAFF (Sno, Fname, Lname ,Address ,Tel_No ,Position, Sex, DOB, Salary, NIN, Bno) STAFF (Sno,Fname,Lname,地址,Tel_No,位置,性别,DOB,薪水,NIN,Bno)
  • PROPERTY_FOR_RENT (Pno, Street, Area, City, Pcode, Type, Rooms, Rent, Ono, Sno, Bno) PROPERTY_FOR_RENT (Pno,街道,区域,城市,Pcode,类型,房间,租金,Ono,Sno,Bno)

A view BranchStats ( Bno, NumStaff, NumProps ) is to be created that, for any branch, indicates the number of staff members employed and the number of properties handles. 将创建一个视图BranchStatsBno, NumStaff, NumPropsBno, NumStaff, NumProps该视图对于任何分支均指示雇用的员工数量和属性句柄的数量。

CREATE VIEW BranchStats
AS 
   SELECT 
       branch.Bno Bno,
       Count (Distinct Staff.Bno) NumStaff,
       Count (Distinct property_for_rent.Sno) NumProps
   FROM 
       branch
   JOIN 
       Staff ON Staff.Bno = branch.Bno
   JOIN 
       property_for_rent ON property_for_rent.Sno = Staff.Sno
   GROUP BY 
       branch.Bno

Error 错误

You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Distinct staff.Bno) NumStaff, Count (Distinct property_for_rent.Sno) Num' at line 4 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第4行“不同的staff.Bno)NumStaff,计数(不同的property_for_rent.Sno)Num附近使用”

Is there any wrong in my SQL statement? 我的SQL语句有什么错误吗? Need help 需要帮忙

MySQL (by default) does not allow a space between the function name and the opening paren. MySQL(默认情况下)不允许在函数名称和开头括号之间使用空格。 So, try removing those spaces: 因此,尝试删除这些空格:

CREATE VIEW BranchStats AS 
   SELECT branch.Bno,
          Count(Distinct Staff.Bno) as NumStaff,
          Count(Distinct property_for_rent.Sno) as NumProps
   FROM branch JOIN 
        Staff
        ON Staff.Bno = branch.Bno JOIN 
        property_for_rent
        ON property_for_rent.Sno = Staff.Sno
   GROUP BY  branch.Bno;

This is explained in the documentation : 文档中对此进行了解释:

[ . [。 . .] the parser uses the following rules by default to distinguish whether their names are being used as function calls or as identifiers in nonexpression context: 。]解析器默认使用以下规则来区分其名称是在非表达式上下文中用作函数调用还是用作标识符:

  • To use the name as a function call in an expression, there must be no whitespace between the name and the following “(” parenthesis character. 要将名称用作表达式中的函数调用,名称和以下“(”)字符之间必须没有空格。

  • Conversely, to use the function name as an identifier, it must not be followed immediately by a parenthesis. 相反,要将函数名称用作标识符,切勿在其后立即加上括号。

The requirement that function calls be written with no whitespace between the name and the parenthesis applies only to the built-in functions that have special considerations. 名称和括号之间不得包含空格的函数调用要求仅适用于具有特殊注意事项的内置函数。 COUNT is one such name. COUNT就是这样一个名字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM