简体   繁体   English

MySQL查询在PHP视图中使用计数

[英]Mysql query for using count on a view in php

I have a query: 我有一个查询:

$result = mysql_query("CREATE VIEW temporary(IngList) AS (
                         SELECT DISTINCT (r1.Ingredient) 
                           FROM recipes r1, 
                                recipes r2 
                          WHERE r1.Country = '$temp' 
                            AND r2.Country = '$temp2' 
                            AND r1.Ingredient = r2.Ingredient) 
                       SELECT COUNT(*) FROM temporary");

I want the query to make a view called temporary and have it return a count of the number of rows in the view temporary. 我希望查询创建一个称为临时的视图,并使其返回该视图中临时行数的计数。 I know this code works without the SELECT COUNT(*) because I checked my database and the view is created. 我知道这段代码无需SELECT COUNT(*)因为我检查了数据库并创建了视图。

Yet this code throws the 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 'SELECT COUNT(*) FROM temporary' at line 1 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第1行的“ SELECT COUNT(*)FROM Temporary”附近使用

I checked the syntax and it seems to be correct. 我检查了语法,这似乎是正确的。 What seems to be the problem because its quite frustrating. 似乎是问题所在,因为它很令人沮丧。

For starters you have two statements. 对于初学者,您有两个声明。 What you're writing looks more like a stored procedure. 您正在编写的内容看起来更像是一个存储过程。 Even if it worked, you would need a semicolon at the end of the first statement. 即使有效,在第一条语句的末尾也需要使用分号。 And another statement somewhere saying "DROP VIEW ...." when you are done. 完成后在另一处显示“ DROP VIEW ....”的语句。

And a temp view is a bit of a non sequitur. 临时视图有点不合时宜。 I can't find any reference to "CREATE VIEW temporary". 我找不到对“创建临时视图”的任何引用。 Or maybe it's to create a view named temporary with an argument? 还是创建带有参数的名为临时的视图? Views don't take arguments. 视图不带参数。

I think you might get what you want with a semi-simple SQL statement something like: 我认为您可以通过半简单的SQL语句获得所需的内容,例如:

$result = mysql_query( $结果= mysql_query(

"SELECT COUNT(DISTINCT r1.Ingredient)
 FROM recipes r1
 JOIN recipes r2 ON r1.Ingredient = r2.Ingredient
 WHERE r1.Country = '$temp' 
     AND r2.Country = '$temp2'");

From the mysql_query documentation : mysql_query文档中

mysql_query() sends a unique query (multiple queries are not supported) ... mysql_query()发送一个唯一查询(不支持多个查询) ...

You can't create the view, and select from it in a single mysql_query. 您无法创建视图,并在单个mysql_query中从中选择视图。 The view is unnecessary: 该视图是不必要的:

$sql = sprintf("SELECT COUNT(DISTINCT r1.Ingredient) 
                  FROM recipes r1
                 WHERE r.country = '%s'
                   AND EXISTS(SELECT NULL
                                FROM recipes r2 
                               WHERE r2.Country = '%s' 
                                 AND r1.Ingredient = r2.Ingredient)",
                $temp, $temp2);

$result = mysql_query($sql);

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

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