SQL使用选择MIN(列)作为where =的输入

时间:2018-05-22 17:30:28

标签: sql

我有一个使用WHERE子句来选择特定值的查询,但是我想使用其他查询的SELECT输出而不是硬编码相等的条件。

工作查询

from model
join upc 
on model.bucket_upc_id = upc.bucket_upc_id
join elect_prop 
on model.id = elect_prop.id
where upc.bucket_upc = 1234455 
 order by elect_prop.i_out as

我想用子查询中的值替换1234455

不工作

from model
join upc 
on model.bucket_upc_id = upc.bucket_upc_id
join elect_prop 
on model.id = elect_prop.id
where upc.bucket_upc = BucketUPC
                       (
        select    bucket.bucket_upc as BucketUPC
                       from model as modelInfo
                           join upc as bucket on 
 bucket.bucket_upc_id=modelInfo.bucket_upc_id
                       where modelInfo.id = 179108
                       ) 
 order by elect_prop.i_out as

2 个答案:

答案 0 :(得分:0)

以这种方式添加子查询:

WHERE Current = (SELECT MIN(X) FROM MY_TABLE)

答案 1 :(得分:0)

这是一个关于如何做的通用示例。如果你给了一些表格结构,我们可能会做得更好,但是现在你去了。

select
    *
from
    TABLE
where
    COLUMN = (select min(COLUMN) from TABLE);