我可以添加AS变量列表吗?

时间:2015-12-28 22:39:17

标签: mysql

甚至不确定如何标题这个问题。我有一个表格列表,其中包含国家狗俱乐部数据库中狗标题的分数。 我使用以下语法在任何至少获得一个标题的场所获得1。这是通过表格上的LEFT JOIN完成的:

code:

SELECT DISTINCT 
    base.dog, base.owner, base.regno, 
    IF((conf.conf_SN_V>0)OR(conf.conf_CH_V>0),1,0) AS Conformation,
    IF((draft.draft_NDD_V>0)OR(draft.draft_NDDV_V>0)OR(draft.draft_BNDD_V>0),1,0) AS Drafting, 
    IF (water.ww_WD_V>0,1,0) AS WaterWork, 
    IF(agility.ag_NA_V>0,1,0) AS Agility, 
    IF ((obed.ob_BN_V>0)OR(obed.ob_CD_V>0),1,0) AS Obedience, 
    IF(rally.ra_RN_V>0,1,0) AS Rally, 
    IF((comm.co_CGCA_V>0)OR(therapy.th_LTA_V>0),1,0) AS Therapy_Comm, 
    IF(nose.nw_NW1_V>0,1,0) AS NoseWork, 
    IF((track.tr_TD_V>0)OR(track.tr_TDU_V>0),1,0) AS Tracking, 
    IF(barn.bh_RATO_V>0,1,0) AS BarnHunt, IF(dive.dd_DNX_V>0,1,0) AS DockDiving, 
    IF(fly.fb_FDCH_V>0,1,0) AS Flyball, IF(course.ca_CAX_V>0,1,0) AS CoursingAb, 
    IF(pack.pk_WPDX_V>0,1,0) AS Packing
FROM base
LEFT JOIN conf ON base.regno=conf.conf_regno
LEFT JOIN draft ON base.regno=draft.draft_regno
LEFT JOIN water ON base.regno=water.ww_regno
LEFT JOIN agility ON base.regno=agility.ag_regno
LEFT JOIN obed ON base.regno=obed.ob_regno
LEFT JOIN rally ON base.regno=rally.ra_regno
LEFT JOIN comm ON base.regno=comm.co_regno
LEFT JOIN therapy ON base.regno=therapy.th_regno
LEFT JOIN nose ON base.regno=nose.nw_regno
LEFT JOIN barn ON base.regno=barn.bh_regno
LEFT JOIN dive ON base.regno=dive.dd_regno
LEFT JOIN fly ON base.regno=fly.fb_regno
LEFT JOIN course ON base.regno=course.ca_regno
LEFT JOIN pack ON base.regno=pack.pk_regno
LEFT JOIN track ON base.regno=track.tr_regno;

现在,我需要添加这些1来计算带标题的场地数量 我尝试在SELECT语句中添加一个计算:Conformation + Drafting + etc AS vTotal

我也尝试过SET vTotal = Conformation + .....

无论如何都要在查询中执行此操作吗?

1 个答案:

答案 0 :(得分:1)

将其放入子查询中,然后在外部查询中添加列。

SELECT *, Conformation+Drafting+Waterwork+... AS totalScore
FROM (
    SELECT DISTINCT 
        base.dog, base.owner, base.regno, 
        IF((conf.conf_SN_V>0)OR(conf.conf_CH_V>0),1,0) AS Conformation,
        IF((draft.draft_NDD_V>0)OR(draft.draft_NDDV_V>0)OR(draft.draft_BNDD_V>0),1,0) AS Drafting, 
        IF (water.ww_WD_V>0,1,0) AS WaterWork, 
        IF(agility.ag_NA_V>0,1,0) AS Agility, 
        IF ((obed.ob_BN_V>0)OR(obed.ob_CD_V>0),1,0) AS Obedience, 
        IF(rally.ra_RN_V>0,1,0) AS Rally, 
        IF((comm.co_CGCA_V>0)OR(therapy.th_LTA_V>0),1,0) AS Therapy_Comm, 
        IF(nose.nw_NW1_V>0,1,0) AS NoseWork, 
        IF((track.tr_TD_V>0)OR(track.tr_TDU_V>0),1,0) AS Tracking, 
        IF(barn.bh_RATO_V>0,1,0) AS BarnHunt, IF(dive.dd_DNX_V>0,1,0) AS DockDiving, 
        IF(fly.fb_FDCH_V>0,1,0) AS Flyball, IF(course.ca_CAX_V>0,1,0) AS CoursingAb, 
        IF(pack.pk_WPDX_V>0,1,0) AS Packing
    FROM base
    LEFT JOIN conf ON base.regno=conf.conf_regno
    LEFT JOIN draft ON base.regno=draft.draft_regno
    LEFT JOIN water ON base.regno=water.ww_regno
    LEFT JOIN agility ON base.regno=agility.ag_regno
    LEFT JOIN obed ON base.regno=obed.ob_regno
    LEFT JOIN rally ON base.regno=rally.ra_regno
    LEFT JOIN comm ON base.regno=comm.co_regno
    LEFT JOIN therapy ON base.regno=therapy.th_regno
    LEFT JOIN nose ON base.regno=nose.nw_regno
    LEFT JOIN barn ON base.regno=barn.bh_regno
    LEFT JOIN dive ON base.regno=dive.dd_regno
    LEFT JOIN fly ON base.regno=fly.fb_regno
    LEFT JOIN course ON base.regno=course.ca_regno
    LEFT JOIN pack ON base.regno=pack.pk_regno
    LEFT JOIN track ON base.regno=track.tr_regno) AS subquery