【问题标题】:mysql pass field value into other select querymysql将字段值传递给其他选择查询
【发布时间】:2021-09-13 04:58:08
【问题描述】:

我的查询代码是这样的,我想将字段 TH_G_RESN_1 的值传递给子查询。请帮帮我谢谢 这部分在硬编码时起作用

SELECT temp.TH_G_RESN_1, temp.TH_G_RESN_2,
(select * from (
        select 3  as num 
        UNION SELECT 2 
) AS smallest order by num asc limit 0,1)
FROM as_thin_reason_detail_data temp WHERE apc_id = 656

当我将TH_G_RESN_1 传递给子查询Unknown column 'TH_G_RESN_1' in 'field list'

SELECT temp.TH_G_RESN_1, temp.TH_G_RESN_2,
(select * from (
        select temp.TH_G_RESN_1  as num 
        UNION SELECT temp.TH_G_RESN_2
) AS smallest order by num asc limit 0,1)
FROM as_thin_reason_detail_data temp WHERE apc_id = 656

【问题讨论】:

  • 删除中间SELECT *

标签: mysql subquery


【解决方案1】:

如果你想要一个特定的记录,默认情况下是最小的,那么你可以按条件排序并获取第一个结果集

SELECT TH_G_RESN_1, TH_G_RESN_2
FROM as_thin_reason_detail_data 
ORDER BY case when apc_id = 656 
              then 1
              else 2 
         end,
         num asc 
limit 1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-25
    • 1970-01-01
    • 2013-04-13
    • 2020-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多