Navicat 用户 - 有什么方法可以在新查询的顶部创建变量并在页面上进行处理吗?

发布于 2024-10-21 21:26:21 字数 742 浏览 6 评论 0原文

我本质上是在尝试创建一个 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 技术交流群。

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

发布评论

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

评论(3

此刻的回忆 2024-10-28 21:26:21

进一步研究我自己的问题后,发现您可以使用以下命令在 sql 调用中设置变量:

SET @variablename = [值]

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:

SET @variablename = [value]

http://dev.mysql.com/doc/refman/5.0/en/user-variables.html

盛装女皇 2024-10-28 21:26:21

这是另一个答案,因为您不想使用 join

SET @@session.var = 'some_value'; 
select * from table where field = @@session.var

尚未测试,但我从这里阅读

http://forums.mysql.com/read.php?61,127011,127796#msg-127796

我认为该变量将被保留,直到连接关闭并重新创建。

编辑:OP找到了答案的其余部分。按要求编辑。

您可以使用以下方法在 SQL 调用中设置变量:

SET @variablename = [value]

http://dev .mysql.com/doc/refman/5.0/en/user-variables.html

This is another answer since you don't want to use join

SET @@session.var = 'some_value'; 
select * from table where field = @@session.var

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:

SET @variablename = [value]

http://dev.mysql.com/doc/refman/5.0/en/user-variables.html

如梦初醒的夏天 2024-10-28 21:26:21

您能提供整个查询吗?我认为如果派生表是 SELECT 语句,则它可能会起作用。

一个简单的例子是从这个开始将

select * from table where field = 'somedate'

其更改为这个

select table.* from (select 'somedate' q)q, table where field = q.q

所以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

select * from table where field = 'somedate'

Change it to this

select table.* from (select 'somedate' q)q, table where field = q.q

So q is your derived table of a single row and single field and q.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.

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