【问题标题】:how update in sql in a join and order by如何在连接和排序中更新sql
【发布时间】:2013-04-24 16:54:28
【问题描述】:

我请教一下:

update enfermedades
    join
pacientes ON pacientes.id = enfermedades.paciente_id
    join
consultas ON consultas.paciente_id = pacientes.id
    join
signos_sintomas ON signos_sintomas.consulta_id = consultas.id 

set 
    enfermedades.diabetes = 1,
    bmi = signos_sintomas.imc_kg_m2,
    promedio_presion_sistolica = ((pa_de_pie_izquierda + pa_sentado_izquierda) / 2),
    microalbuminuria = 0,
    macroalbuminuria = 0,
    fibrilacion = 0,
    ecv = 0,
    duracion = 15,
    antiht = 0,
    diabetes = 0
where
    enfermedades.riesgo_diabetes_mellitus = 0
        and pacientes.situacion = 0
        and riesgo_diabetes_dano_organo_blanco = 0
        and signos_sintomas.created in (select 
            signos_sintomas.created,consultas.created
        from
            signos_sintomas
                join
            consultas ON consultas.id = signos_sintomas.consulta_id
                join
            pacientes ON pacientes.id = consultas.paciente_id
        where
            pacientes.id = pacientes.id
        order by signos_sintomas.created , consultas.created desc);

错误 1241 (21000):操作数应包含 1 列

作为咨询,但按创建列的降序排列

【问题讨论】:

    标签: mysql sql sql-update


    【解决方案1】:

    您在返回多个列的 IN 子句中使用子选择。这样的子选择只能返回 ONE 列,用于IN() 比较:

    and signos_sintomas.created in (select 
                signos_sintomas.created,consultas.created
                        ^^^--field #1       ^---field #2
            from
    

    如果您返回多个列,则数据库服务器无法知道应该使用哪个列进行IN() 比较。

    【讨论】:

    • 谢谢,但我需要更新。并说:匹配的行:960 更改:0 警告:0
    • 那么你将不得不重建你的查询。一般来说,大多数子选择都可以写成普通的 join+where 查询,这样您就可以根据需要使用子查询表中的尽可能多的字段。
    【解决方案2】:

    改变这个

       and signos_sintomas.created in (select 
            signos_sintomas.created,consultas.created
    

       and signos_sintomas.created in (select 
            signos_sintomas.created
    

    您只能选择一列,而不是两列。

    【讨论】:

      猜你喜欢
      • 2015-08-18
      • 1970-01-01
      • 1970-01-01
      • 2011-03-28
      • 2010-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-30
      相关资源
      最近更新 更多