简体   繁体   English

PHP语法的初学者,mySQL_query错误

[英]Beginner at PHP syntax, mySQL_query error

I'm attempting to make a page that is just a CREATE VIEW using DreamWeaver CS5. 我正在尝试使用DreamWeaver CS5制作一个仅创建视图的页面。 How do I need to input my code to just receive a table with all data relevant to the logged in user. 我如何输入代码以仅接收包含与登录用户有关的所有数据的表。

mysql_query(
CREATE VIEW UserResults
AS 
   SELECT E.No, E. Description
      , Count(R.RID WHERE $Username= R.Owner)
      , Count(R.RID WHERE $Username= R.Owner AND Status==’Active’ )
   FROM ETable AS E, RTABLE as R
   ORDER BY E.No)

您必须将查询放在引号中以使其成为字符串。

mysql_query("CREATE VIEW UserResults AS SELECT E.No, E. Description, Count(R.RID WHERE $Username= R.Owner), Count(R.RID WHERE $Username= R.Owner AND Status==’Active’ ) FROM ETable AS E, RTABLE as R ORDER BY E.No")

Daniel is correct about the quotes. 丹尼尔对引号是正确的。 However, you should probably take a look at PDO , the mysql_ functions are a bit outdated. 但是,您可能应该看看PDO ,mysql_函数有些过时了。 You can read more about that here . 您可以在此处阅读更多有关此内容的信息

  • You have an extra ending parenthesis, which should be removed 您有一个多余的结尾括号,应将其删除
  • Views should not have ORDER BY . 视图不应具有ORDER BY
  • The WHERE clause should not be in the middle of the SELECT , you will need to use a CASE . WHERE子句不应位于SELECT的中间,您将需要使用CASE
  • You are returning a Cartesian product instead of joining your tables together, this may provide more results than you want/expect. 您要返回的是笛卡尔积而不是将表连接在一起,这可能会提供比您期望的结果更多的结果。

The R.RID in the THEN block of the CASE statements may need to be a 1 , not entirely sure what you hoped to get from those COUNT statements you originally had. CASE语句的THEN块中的R.RID可能需要为1 ,并不完全确定您希望从最初拥有的COUNT语句中得到什么。

I tried to figure out what you were trying to do, and I think this is it: 我试图弄清楚您要做什么,我想就是这样:

CREATE VIEW UserResults AS
   SELECT E.No, E.Description
      , SUM(CASE 
               WHEN $Username = R.Owner THEN R.RID
               ELSE 0
            END) AS SumOfOwner
      , SUM(CASE
               WHEN $Username = R.Owner AND Status = 'Active' THEN R.RID
               ELSE 0
            END) AS SumOfOwnerAndActive
   FROM ETable AS E
   INNER JOIN RTABLE as R ON E.No = R.ENo
   GROUP BY E.No, E.Description

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

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