donglan9517 2015-05-20 17:43
浏览 34
已采纳

从表mysql中选择子记录

I got the bellow piece of select statement that got level 2 child records, having problems to got deeper, can anyone help out?

SELECT
  id_mobile AS ID_PROJETO,
  UM.qtd_UC,
  AM.qtd_AMBIENTE
FROM projetos_mobile AS PM
LEFT JOIN (
   SELECT
      COUNT(id) AS qtd_UC,
      projeto,
      data_hora_importacao,
      id_uc_mobile
   FROM ucs_mobile
   WHERE data_hora_importacao = '2015-05-15 17:21:02'
   GROUP BY projeto) AS UM
ON PM.id_mobile = UM.projeto
LEFT JOIN (
   SELECT
      COUNT(id_uc_mobile) AS qtd_AMBIENTE,
      id_uc_mobile
   FROM ucs_mobile
   LEFT JOIN (
      SELECT
         uc
      FROM ambientes_mobile AS s
      WHERE data_hora_importacao = '2015-05-15 17:21:02') AS G
   ON G.uc = ucs_mobile.id_uc_mobile
   WHERE data_hora_importacao = '2015-05-15 17:21:02') AS AM
ON UM.id_uc_mobile = AM.id_uc_mobile
WHERE PM.data_hora_importacao = '2015-05-15 17:21:02'

http://sqlfiddle.com/#!9/2eecf

here is a sqlfiddle if anyone want to try a solution. I have the specific hierarchy: projeto>uc>ambiente>secao>medicoes

ucs_mobile.projeto refers to projetos_mobile.id_mobile
ambientes_mobile.uc refers to ucs_mobile.id_uc_mobile
secoes_iluminacao_mobile.ambiente refers to ambientes_mobile.id_ambiente_mobile

I need a count of each child for the parent I pass, I will have 5 functions that return the count of each child for a given parent, for example, for a projeto parent I should have count(ucs),count(ambientes),count(secoes),count(medicoes)

So, hope you guys can help me. The database is terrible ugly but that's is what I got. Appreciate any help.

  • 写回答

1条回答 默认 最新

  • doulanli6146 2015-05-20 18:14
    关注

    When you have really large queries like this, it can often be helpful to break them down individually, starting from the ground up and patching them together.

    I started by just getting the count of each ucs_mobile row for each projetos_mobile value. You can do that by joining the two tables on the related row, and using COUNT(DISTINCT um.id) to get the number of rows. There are other ways to do it, but this particular method will scale better for the rest of your query:

    SELECT pm.id, COALESCE(COUNT(DISTINCT um.id), 0) AS qty_uc
    FROM projetos_mobile pm
    LEFT JOIN ucs_mobile um ON um.data_hora_importacao = '2015-05-15 17:21:02' AND um.projeto = pm.id_mobile
    GROUP BY pm.id;
    

    The COALESCE function will be used to fill 0 counts. As long as you remember to use the DISTINCT keyword, and group by the proper id, you can just add in the child rows like so:

    SELECT 
      pm.id, 
      COALESCE(COUNT(DISTINCT um.id), 0) AS qty_uc, 
      COALESCE(COUNT(DISTINCT am.id), 0) AS qty_am,
      COALESCE(COUNT(DISTINCT sim.id), 0) AS qty_sim
    FROM projetos_mobile pm
    LEFT JOIN ucs_mobile um ON um.data_hora_importacao = '2015-05-15 17:21:02' AND um.projeto = pm.id_mobile
    LEFT JOIN ambientes_mobile am ON am.data_hora_importacao = um.data_hora_importacao AND am.uc = um.id_uc_mobile
    LEFT JOIN secoes_iluminacao_mobile sim ON sim.data_hora_importacao = am.data_hora_importacao AND sim.ambiente = am.id_ambiente_mobile
    GROUP BY pm.id;
    

    Here is an SQL Fiddle example. NOTE I changed your sample data slightly to ensure my query was working as expected.

    Also, a side note. I noticed as you went along that you kept using the same date in your WHERE clauses, so I just joined each table on the date as well, and made sure that in my very first join I looked for the date specified, which in turn will carry its way over to the other tables.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏