在 php 中使用视图计数的 Mysql 查询
我有一个查询:
$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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自 mysql_query 文档:
您无法创建视图,并在单个 mysql_query 中从中进行选择。该视图是不必要的:
From the mysql_query documentation:
You can't create the view, and select from it in a single mysql_query. The view is unnecessary:
首先,你有两个陈述。你写的看起来更像是一个存储过程。即使它有效,您也需要在第一个语句的末尾添加分号。当你完成时,还有另一句话说“删除视图......”。
临时视图有点不合逻辑。我找不到任何对“临时创建视图”的引用。或者也许是创建一个带有参数的名为临时的视图?观点不接受争论。
我想你可能会通过半简单的 SQL 语句得到你想要的东西,比如:
$result = mysql_query(
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(