在where子句中的MySQL Select Statment中使用变量 [英] Using a variable in MySQL Select Statment in a Where Clause

查看:140
本文介绍了在where子句中的MySQL Select Statment中使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能在select语句中找到一个值并将其用于where子句中,例如:

I would like to know if it's possible to find a value in a select statement and use it in a where clause like:

SELECT col1, MAX(col2) - COUNT(DISTINCT col3) as variable
FROM table
WHERE col1 > variable

表非常大,我想缩小查询必须查找的记录尽快

"table" is pretty big, and I want to narrow down the records the query has to look at as quickly as possible.

我知道这不是写代码所无法实现的代码(#1054- where子句中的未知列 variable),但是仍然存在找出变量的值,然后在WHERE子句中使用它?

I know this isn't bit of code isn't possible as written (#1054 - Unknown column 'variable' in 'where clause'), but is there anyway to figure out the value of "variable" and then use it in the WHERE clause?

推荐答案

您也可以尝试使用子查询语法

You could try subquery syntax, also called nested select.

我认为是这样的:

SELECT col1 WHERE col1 > (SELECT MAX(col2) - COUNT(DISTINCT col3)) 

请参见 MySQL手册以获取一些更好的示例。

See the MySQL manual for some better examples.

这篇关于在where子句中的MySQL Select Statment中使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆