SQL:在这种情况下如何按 sql 顺序查询

SQL: how query in sql order by created in this case(SQL:在这种情况下如何按 sql 顺序查询)
本文介绍了SQL:在这种情况下如何按 sql 顺序查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

select 
    consultas.id as a,
    consultas.clasificacion as cl,
    consultas.paciente_id as b,
    consultas.created,imc_kg_m2 as im
from
    consultas
join 
    signos_sintomas
    on 
        signos_sintomas.consulta_id = consultas.id
    where paciente_id in
    (select pacientes.id from pacientes where pacientes.id < 30 order by pacientes.id desc)
    and
        consultas.created in 
        (select consultas.created from consultas order by consultas.created asc  )
    and 
        imc_kg_m2 is not null

    ;  



 +-----+----+----+---------------------+------+
    | a   | cl | b  | created             | im   |
    +-----+----+----+---------------------+------+
    | 198 | 1  |  1 | 2012-07-25 00:00:00 | 25.6 |
    |   2 | 1  |  2 | 2012-05-30 00:00:00 | 25.5 |
    |   2 | 1  |  2 | 2012-05-30 00:00:00 | 25.5 |
    |   3 | 1  |  3 | 2012-05-30 00:00:00 | 21.1 |
    |   3 | 1  |  3 | 2012-05-30 00:00:00 | 21.3 |
    | 579 | 2  |  4 | 2012-10-03 00:00:00 | 22.9 |
    | 579 | 2  |  4 | 2012-10-03 00:00:00 | 22.9 |
    | 127 | 1  |  5 | 2012-07-16 00:00:00 | 28.7 |
    | 127 | 1  |  5 | 2012-07-16 00:00:00 | 28.9 |
    | 127 | 1  |  5 | 2012-07-16 00:00:00 | 28.9 |
    | 498 | 1  |  6 | 2012-09-19 00:00:00 | 29.7 |
    | 498 | 1  |  6 | 2012-09-19 00:00:00 | 29.7 |
    | 200 | 1  |  7 | 2012-07-25 00:00:00 |    0 |
    | 149 | 1  |  8 | 2012-07-18 00:00:00 | 23.6 |
    | 149 | 1  |  8 | 2012-07-18 00:00:00 | 23.6 |
    |   4 | 1  |  9 | 2012-06-16 00:00:00 |    0 |
    |   4 | 1  |  9 | 2012-06-16 00:00:00 | 24.9 |
    |   5 | 2  | 10 | 2012-06-26 00:00:00 | 24.9 |
    |   7 | 2  | 11 | 2012-06-26 00:00:00 |   25 |
    |   6 | 2  | 12 | 2012-06-26 00:00:00 | 24.8 |
    | 648 | 2  | 13 | 2012-10-22 00:00:00 | 27.5 |
    |   8 | 2  | 14 | 2012-06-26 00:00:00 |   34 |
    |   8 | 2  | 14 | 2012-06-26 00:00:00 | 32.8 |
    |   9 | 2  | 15 | 2012-06-26 00:00:00 | 32.8 |
    |   9 | 2  | 15 | 2012-06-26 00:00:00 | 32.8 |
    |   9 | 2  | 15 | 2012-06-26 00:00:00 | 32.2 |
    |  10 | 2  | 16 | 2012-06-26 00:00:00 | 29.8 |
    |  10 | 2  | 16 | 2012-06-26 00:00:00 | 30.4 |
    |  11 | 2  | 17 | 2012-06-26 00:00:00 | 28.1 |
    |  11 | 2  | 17 | 2012-06-26 00:00:00 | 28.1 |
    | 156 | 2  | 19 | 2012-07-18 00:00:00 | 29.5 |
    |  12 | 2  | 20 | 2012-06-26 00:00:00 |    0 |
    |  12 | 2  | 20 | 2012-06-26 00:00:00 |    0 |
    |  12 | 2  | 20 | 2012-06-26 00:00:00 |    0 |
    |  13 | 1  | 21 | 2012-06-26 00:00:00 |   29 |
    |  13 | 1  | 21 | 2012-06-26 00:00:00 |   29 |
    |  14 | 3  | 22 | 2012-06-26 00:00:00 | 32.5 |
    |  15 | 2  | 23 | 2012-06-26 00:00:00 | 30.4 |
    |  16 | 2  | 24 | 2012-06-26 00:00:00 |    0 |
    |  17 | 11 | 25 | 2012-06-26 00:00:00 |   28 |
    |  18 | 2  | 26 | 2012-06-26 00:00:00 | 23.9 |
    |  19 | 1  | 27 | 2012-06-27 00:00:00 |   32 |
    |  19 | 1  | 27 | 2012-06-27 00:00:00 |   32 |
    |  19 | 1  | 27 | 2012-06-27 00:00:00 | 34.4 |
    |  21 | 1  | 28 | 2012-06-27 00:00:00 | 29.1 |
    |  21 | 1  | 28 | 2012-06-27 00:00:00 | 29.1 |
    |  23 | 1  | 29 | 2012-06-27 00:00:00 | 26.8 |
    |  23 | 1  | 29 | 2012-06-27 00:00:00 | 26.3 |
    +-----+----+----+---------------------+------+

我尝试限制但说:ERROR 1235 (42000): 这个版本的 MySQL 还不支持 'LIMIT &IN/ALL/ANY/SOME 子查询'我只需要查询一个 imc_kg_m2 值但最近创建的

I try to make a limit but Say: ERROR 1235 (42000): This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery' i need the query only one imc_kg_m2 value but most recent created

update pacientes
        join
    enfermedades 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 
    bmi = signos_sintomas.imc_kg_m2,
    enfermedades.promedio_presion_sistolica = ((pa_de_pie_izquierda + pa_sentado_izquierda) / 2),
    enfermedades.microalbuminuria = 0,
    enfermedades.macroalbuminuria = 0,
    enfermedades.fibrilacion = 0,
    enfermedades.ecv = 0,
    enfermedades.duracion = 15,
    enfermedades.antiht = 0,
    enfermedades.diabetes = 1
where
    enfermedades.riesgo_diabetes_mellitus = 1
        and pacientes.situacion = 0
        and riesgo_diabetes_dano_organo_blanco = 0
        and signos_sintomas.created in (select 
            signos_sintomas.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);

值 null 我很自然,我需要更新 enfermedades 中最近的 imc_kg_m2

the values null i'ts natural by i'm need the recent imc_kg_m2 in the update enfermedades

+------+-----+------+------+------+------+------+------+------+------+
| bmi  | p   | d1   | d2   | d3   | ecv  | m1   | m2   | f    | pa   |
+------+-----+------+------+------+------+------+------+------+------+
| NULL |   1 |    0 |    0 |    0 | 0    | 0    | 0    | 0    |  132 |
| NULL |   3 |    0 |    0 |    0 | 0    | 0    | 0    | 0    |  105 |
| NULL |  30 |    0 |    0 |    0 | NULL | NULL | 0    | NULL |  160 |
| NULL | 153 |    0 |    0 |    0 | NULL | NULL | 0    | NULL |  160 |
| NULL | 175 |    0 |    0 |    0 | NULL | NULL | 0    | NULL |  160 |
| NULL | 343 |    0 |    0 |    0 | NULL | NULL | 0    | NULL |  160 |
| NULL | 520 |    0 |    0 |    0 | NULL | NULL | 0    | NULL |  160 |
| NULL | 867 |    0 |    0 |    0 | NULL | NULL | 0    | NULL |  160 |
+------+-----+------+------+------+------+------+------+------+------+

我开始怀疑这样的咨询不能解决我的问题,依赖关系和现在存在的关系,我不能做任何其他方式来改变它会影响我的模型和视图模型.

I begin to suspect that such consultation is not the solution to my problem, dependencies and the relationship that exists right now, I can not do it any other way to change it would affect me the model and the view model.

推荐答案

我不确定我是否完全理解目标,但我试了一下......

Not sure I totally understand the aim, but I had a go...

select 
consultas.id as a,
consultas.clasificacion as cl,
consultas.paciente_id as b,
consultas.created,
imc_kg_m2 as im
from consultas
join signos_sintomas on signos_sintomas.consulta_id = consultas.id
join (select pacientes.id from pacientes where pacientes.id < 30) p on consultas.paciente_id = p.id
where imc_kg_m2 = (select imc_kg_m2 from consultas where created = (select max(created) from consultas where imc_kg_m2 is not null)); 

为了解决这个错误,我为子查询设置了别名并加入了它们.我删除了一个,因为我无法弄清楚它为什么在那里.我还添加了 where 来执行您要求的操作.如果它不符合您的要求,请发表评论.

To resolve the error I aliased the subqueries and joined them instead. I removed one because I couldn't work out why it was there. I also added the where to do something like what you are requesting. Comment if it doesn't do what you want.

这篇关于SQL:在这种情况下如何按 sql 顺序查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)