MySQL使用教程(六)

3.7. 目的查询

3.7.1. 找所有未分

3.7.2. 的表

Institute of Environmental Medicine atKarolinska Institutet Stockholm  the Section on Clinical Research in Aging andPsychology at the University of Southern California的合作目。

该项目包括筛选部分,即通过电话访在瑞典超 65 的所有生。足某种标准的入下一段。在下一段中,医/士小访问想参加的生。部分检查包括物理检查和神、心理检查实验试验、神成像、心理状况估和家族史搜集。并且,根据医风险因素来搜集数据。

可从以下接找到生研究的更多信息:

http://www.mep.ki.se/twinreg/index_en.html

用一个用PerlMySQLweb接口来管理目的后面部分。

上所有会的数据被移入一MySQL数据

3.7.1. 找所有未分

下列查询用来决定谁进目的第二部分:

SELECT

   CONCAT(p1.id, p1.tvab) + 0 AS tvid,

   CONCAT(p1.christian_name, ' ', p1.surname) AS Name,

   p1.postal_code AS Code,

   p1.city AS City,

   pg.abrev AS Area,

   IF(td.participation = 'Aborted', 'A', ' ') AS A,

   p1.dead AS dead1,

   l.event AS event1,

   td.suspect AS tsuspect1,

   id.suspect AS isuspect1,

   td.severe AS tsevere1,

   id.severe AS isevere1,

   p2.dead AS dead2,

   l2.event AS event2,

   h2.nurse AS nurse2,

   h2.doctor AS doctor2,

   td2.suspect AS tsuspect2,

   id2.suspect AS isuspect2,

   td2.severe AS tsevere2,

   id2.severe AS isevere2,

   l.finish_date

FROM

   twin_project AS tp

    /*For Twin 1 */

   LEFT JOIN twin_data AS td ON tp.id = td.id

             AND tp.tvab = td.tvab

   LEFT JOIN informant_data AS id ON tp.id = id.id

             AND tp.tvab = id.tvab

   LEFT JOIN harmony AS h ON tp.id = h.id

             AND tp.tvab = h.tvab

   LEFT JOIN lentus AS l ON tp.id = l.id

             AND tp.tvab = l.tvab

    /*For Twin 2 */

   LEFT JOIN twin_data AS td2 ON p2.id = td2.id

              AND p2.tvab = td2.tvab

   LEFT JOIN informant_data AS id2 ON p2.id = id2.id

             AND p2.tvab = id2.tvab

   LEFT JOIN harmony AS h2 ON p2.id = h2.id

             AND p2.tvab = h2.tvab

   LEFT JOIN lentus AS l2 ON p2.id = l2.id

             AND p2.tvab = l2.tvab,

   person_data AS p1,

   person_data AS p2,

   postal_groups AS pg

WHERE

    /*p1 gets main twin and p2 gets his/her twin. */

    /*ptvab is a field inverted from tvab */

   p1.id = tp.id AND p1.tvab = tp.tvab AND

    p2.id= p1.id AND p2.ptvab = p1.tvab AND

    /*Just the screening survey */

   tp.survey_no = 5 AND

    /*Skip if partner died before 65 but allow emigration (dead=9) */

   (p2.dead = 0 OR p2.dead = 9 OR

    (p2.dead = 1 AND

     (p2.death_date = 0 OR

       (((TO_DAYS(p2.death_date) -TO_DAYS(p2.birthday)) / 365)

       >= 65))))

    AND

    (

    /*Twin is suspect */

   (td.future_contact = 'Yes' AND td.suspect = 2) OR

    /*Twin is suspect - Informant is Blessed */

   (td.future_contact = 'Yes' AND td.suspect = 1

                               AND id.suspect =1) OR

    /*No twin - Informant is Blessed */

   (ISNULL(td.suspect) AND id.suspect = 1

                        AND id.future_contact ='Yes') OR

    /*Twin broken off - Informant is Blessed */

   (td.participation = 'Aborted'

    AND id.suspect = 1 AND id.future_contact = 'Yes') OR

    /*Twin broken off - No inform - Have partner */

   (td.participation = 'Aborted' AND ISNULL(id.suspect)

                                  AND p2.dead =0))

    AND

   l.event = 'Finished'

    /*Get at area code */

    ANDSUBSTRING(p1.postal_code, 1, 2) = pg.code

    /*Not already distributed */

    AND(h.nurse IS NULL OR h.nurse=00 OR h.doctor=00)

    /*Has not refused or been aborted */

    ANDNOT (h.status = 'Refused' OR h.status = 'Aborted'

    ORh.status = 'Died' OR h.status = 'Other')

ORDER BY

   tvid;

一些解

·         CONCAT(p1.id,p1.tvab) + 0 AS tvid

想要idtvab接上以数字序排序。0使得MySQL变为一个数字。

·         id

这标识对孪生。它是所有表中的一个

·         tvab

这标识孪生中的一个。它的值为12

·         ptvab

tvab一个逆运算。当tvab1,它是2,反之亦然。它用来保存入并且使MySQL查询更容易。

查询表明,怎联结(p1p2)从同一个表中找表。在例子中,被用来检查孪生的一个是否65前死了。如果如此,行不返回

上述所有生信息存在于所有表中。我们对id,tvab(所有表)和id,ptvab (person_data) 上采用以使查询更快。

在我的生机器(一台200MHzUltraSPARC)查询返回大 150-200 行并且时间不超一秒。

行数

person_data

71074

lentus

5291

twin_project

5286

twin_data

2012

informant_data

663

harmony

381

postal_groups

100

3.7.2. 的表

一次会面以一个称event的状态码结束。下面示的查询被用来示按事件合的所有生的表。表明多少对孪生已完成,多少的其中之一已完成而另一个拒了,等等

SELECT

       t1.event,

       t2.event,

       COUNT(*)

FROM

       lentus AS t1,

       lentus AS t2,

       twin_project AS tp

WHERE

        /*We are looking at one pair at a time */

       t1.id = tp.id

       AND t1.tvab=tp.tvab

        ANDt1.id = t2.id

        /*Just the screening survey */

       AND tp.survey_no = 5

        /*This makes each pair only appear once */

       AND t1.tvab='1' AND t2.tvab='2'

GROUP BY

       t1.event, t2.event;

3.8. Apache一起使用MySQL

有一些目,你可以MySQL数据库鉴别,并且你可以将日志文件写MySQL数据

你可以将以下内容放到Apache配置文件中,更改Apache日志格式,使MySQL更容易取:

LogFormat\

       "\"%h\",%{%Y%m%d%H%M%S}t,%>s,\"%b\",\"%{Content-Type}o\", \

       \"%U\",\"%{Referer}i\",\"%{User-Agent}i\""

要想将格式的日志文件装MySQL,你可以使用以下:

LOAD DATAINFILE '/local/access_log' INTO TABLE tbl_name

FIELDSTERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\'

建的表中的列与写入日志文件LogFormat对应


MySQL参考手册的翻版本,MySQL参考手册,请访问dev.mysql.com原始参考手册英文版,与英文版参考手册相比,本翻版可能不是最新的

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值