Navicat 用户 - 有什么方法可以在新查询的顶部创建变量并在页面上进行处理吗?
我本质上是在尝试创建一个 pHp pdo 参数绑定等效项,而根本不使用 php。
我使用 Navicat 进行 sql 查询和构建,因此我能够做一些比 phpmyadmin 更有趣的事情。
我有一个查询,它接受一个日期值并将其应用于(字面意思)14 个 if、where 和 group by 语句。我想本质上在页面顶部创建一个变量,说:
:date
然后在查询中应用变量说:
WHERE date_inserted > :date
我相信这可以在存储过程中完成,但我只想保留为查询(在查询编辑器)
编辑 - 示例:
在 php 中,您可以使用 pdo 创建准备好的 sql 查询,如下所示:
$stmt = $db->prepare("SELECT * FROM tablename WHERE field1 = :field1 AND datefield <= :date1 AND datefield2 >= :date1 AND datefield3 > :date1");
$stmt->bindParam(':field1', $somevariable);
$stmt->bindParam(':date1', $somedate);
$stmt->execute();
我想仅使用 sql 执行完全相同的操作(使用 navicat)
I am essentially trying to create a pHp pdo parameter binding equivalent without the use of php at all.
I use Navicat for my sql queries and building and as such am able to do some more interesting things than say just phpmyadmin.
I have one query in particular which takes a date value and applies it to (literally) 14 if, where, and group by statements. I would like to essentially create a variable at the top of the page, say:
:date
then within the query, apply the variable say:
WHERE date_inserted > :date
I believe this could be done in a stored procedure but I'd like to just keep as a query (within query editor)
edit - example:
In php, you can create a prepared sql query using pdo like the following:
$stmt = $db->prepare("SELECT * FROM tablename WHERE field1 = :field1 AND datefield <= :date1 AND datefield2 >= :date1 AND datefield3 > :date1");
$stmt->bindParam(':field1', $somevariable);
$stmt->bindParam(':date1', $somedate);
$stmt->execute();
I want to do the exact same thing with sql only (using navicat)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
进一步研究我自己的问题后,发现您可以使用以下命令在 sql 调用中设置变量:
http://dev.mysql. com/doc/refman/5.0/en/user-variables.html
After looking further into my own question, found you can set variables within a sql call using:
http://dev.mysql.com/doc/refman/5.0/en/user-variables.html
这是另一个答案,因为您不想使用 join
尚未测试,但我从这里阅读
http://forums.mysql.com/read.php?61,127011,127796#msg-127796
我认为该变量将被保留,直到连接关闭并重新创建。
编辑:OP找到了答案的其余部分。按要求编辑。
您可以使用以下方法在 SQL 调用中设置变量:
http://dev .mysql.com/doc/refman/5.0/en/user-variables.html
This is another answer since you don't want to use join
Have not tested but I read from here
http://forums.mysql.com/read.php?61,127011,127796#msg-127796
I think the variable will be preserved until the connection is closed and recreated.
Edit: OP found the rest of the answer. Editing in as requested.
You can set variables within an SQL call using:
http://dev.mysql.com/doc/refman/5.0/en/user-variables.html
您能提供整个查询吗?我认为如果派生表是 SELECT 语句,则它可能会起作用。
一个简单的例子是从这个开始将
其更改为这个
所以
q
是单行和单个字段的派生表,qq
是您的字段。如果没有看到更多的声明,我真的不知道还有什么更有用的。
顺便说一句,我对 PHP、phpmyadmin、Navicat 或存储过程一无所知。只能自学MySQL。
Can you provide the whole query? I think a derived table might work if it is a SELECT statement.
A simple example would be to start with this
Change it to this
So
q
is your derived table of a single row and single field andq.q
is your field.I really don't know anything more helpful without seeing more of the statement.
By the way, I know nothing about PHP, phpmyadmin, Navicat or stored procedure. Only self taught MySQL.