在 php 中使用视图计数的 Mysql 查询

发布于 2024-11-02 13:12:53 字数 765 浏览 6 评论 0原文

我有一个查询:

$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");

我希望查询创建一个名为临时的视图,并让它返回临时视图中的行数。我知道这段代码无需 SELECT COUNT(*) 即可工作,因为我检查了数据库并创建了视图。

然而这段代码抛出了错误:

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行的“SELECT COUNT(*) FROMtemporary”附近使用的正确语法

。我检查了语法,它似乎是正确的。这似乎是问题所在,因为它非常令人沮丧。

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.

Yet this code throws the error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT COUNT(*) FROM temporary' at line 1

I checked the syntax and it seems to be correct. What seems to be the problem because its quite frustrating.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宁愿没拥抱 2024-11-09 13:12:53

来自 mysql_query 文档

mysql_query() 发送唯一查询(不支持多个查询)...

您无法创建视图,并在单个 mysql_query 中从中进行选择。该视图是不必要的:

$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);

From the mysql_query documentation:

mysql_query() sends a unique query (multiple queries are not supported)...

You can't create the view, and select from it in a single 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);
满意归宿 2024-11-09 13:12:53

首先,你有两个陈述。你写的看起来更像是一个存储过程。即使它有效,您也需要在第一个语句的末尾添加分号。当你完成时,还有另一句话说“删除视图......”。

临时视图有点不合逻辑。我找不到任何对“临时创建视图”的引用。或者也许是创建一个带有参数的名为临时的视图?观点不接受争论。

我想你可能会通过半简单的 SQL 语句得到你想要的东西,比如:

$result = 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'");

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.

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:

$result = 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'");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文