多个MySQL JOIN和重复的单元格

时间:2016-10-14 00:35:42

标签: python mysql sql pandas

我有两个MySQL查询,可以提供我正在寻找的结果。我最终想要将这两个查询结合起来制作一张桌子并且我被卡住了。

QUERY 1:

SELECT scoc.isr, outcome_concept_id, concept_name as outcome_name
FROM standard_case_outcome AS sco INNER JOIN concept AS c
ON sco.outcome_concept_id = c.concept_id
INNER JOIN standard_case_outcome_category AS scoc
ON scoc.isr = sco.isr
WHERE scoc.outc_code = 'CA'

结果1:

RESULT OF QUERY 1 (TRUNCATED)

QUERY 2:

SELECT scoc.isr, drug_seq, concept_id, concept_name as drug_name
FROM standard_case_drug AS scd INNER JOIN concept AS c
ON scd.standard_concept_id = c.concept_id
INNER JOIN standard_case_outcome_category AS scoc
ON scoc.isr = scd.isr
WHERE scoc.outc_code = 'CA'

结果2:

RESULT OF QUERY 2 (TRUNCATED)

期望的结果: DESIRED RESULT

我很确定我可以弄清楚如何使用Python / pandas来做这件事,但我想知道是否有(a)方法来做这个MySQL(b)使用MySQL做任何好处。

**如果你有点好奇,this is the entire dataset

这里是相关表格的数据库结构:

# Dump of table concept
# ------------------------------------------------------------

CREATE TABLE `concept` (
  `concept_id` int(11) NOT NULL,
  `concept_name` varchar(255) NOT NULL,
  `domain_id` varchar(20) NOT NULL,
  `vocabulary_id` varchar(20) NOT NULL,
  `concept_class_id` varchar(20) NOT NULL,
  `standard_concept` varchar(1) DEFAULT NULL,
  `concept_code` varchar(50) NOT NULL,
  `valid_start_date` date NOT NULL,
  `valid_end_date` date NOT NULL,
  `invalid_reason` varchar(1) DEFAULT NULL,
  PRIMARY KEY (`concept_id`),
  UNIQUE KEY `idx_concept_concept_id` (`concept_id`),
  KEY `idx_concept_code` (`concept_code`),
  KEY `idx_concept_vocabluary_id` (`vocabulary_id`),
  KEY `idx_concept_domain_id` (`domain_id`),
  KEY `idx_concept_class_id` (`concept_class_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;



# Dump of table standard_case_drug
# ------------------------------------------------------------

CREATE TABLE `standard_case_drug` (
  `primaryid` varchar(512) DEFAULT NULL,
  `isr` varchar(512) DEFAULT NULL,
  `drug_seq` varchar(512) DEFAULT NULL,
  `role_cod` varchar(512) DEFAULT NULL,
  `standard_concept_id` int(11) DEFAULT NULL,
  KEY `idx_standard_case_drug_primary_id` (`primaryid`(255),`drug_seq`(255)),
  KEY `idx_standard_case_drug_isr` (`isr`(255),`drug_seq`(255)),
  KEY `idx_standard_case_drug_standard_concept_id` (`standard_concept_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;



# Dump of table standard_case_outcome
# ------------------------------------------------------------

CREATE TABLE `standard_case_outcome` (
  `primaryid` varchar(512) DEFAULT NULL,
  `isr` varchar(512) DEFAULT NULL,
  `pt` varchar(512) DEFAULT NULL,
  `outcome_concept_id` int(11) DEFAULT NULL,
  `snomed_outcome_concept_id` int(11) DEFAULT NULL,
  KEY `idx_standard_case_outcome_primary_id` (`primaryid`(255)),
  KEY `idx_standard_case_outcome_isr` (`isr`(255)),
  KEY `idx_standard_case_outcome_outcome_concept_id` (`outcome_concept_id`),
  KEY `idx_standard_case_outcome_snomed_outcome_concept_id` (`snomed_outcome_concept_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;



# Dump of table standard_case_outcome_category
# ------------------------------------------------------------

CREATE TABLE `standard_case_outcome_category` (
  `primaryid` varchar(512) DEFAULT NULL,
  `isr` varchar(512) DEFAULT NULL,
  `outc_code` varchar(512) DEFAULT NULL COMMENT 'Code for a patient outcome (See table below) CODE MEANING_TEXT ----------------DE Death LT Life-ThreateningHO Hospitalization - Initial or ProlongedDS DisabilityCA Congenital AnomalyRI Required Intervention to Prevent PermanentImpairment/DamageOT Other Serious (Important Medical Event) NOTE: The outcome from the latest version of a case is provided. If there is more than one outcome, the codes willbe line listed.',
  `snomed_concept_id` int(11) DEFAULT NULL,
  KEY `idx_standard_case_outcome_category_primary_id` (`primaryid`(255)),
  KEY `idx_standard_case_outcome_category_isr` (`isr`(255)),
  KEY `idx_standard_case_outcome_category_snomed_concept_id` (`snomed_concept_id`,`outc_code`(255))
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

1 个答案:

答案 0 :(得分:1)

这可以让你得到理想的结果。

SELECT
    `scoc`.`isr`                AS `isr`,
    `sco` .`outcome_concept_id` AS `outcome_concept_id`,
    `c1`  .`concept_name`       AS `outcome_name`,
    `scd` .`drug_seq`           AS `drug_seq`,
    `scd` .`concept_id`         AS `concept_id`,
    `c2`  .`concept_name`       AS `drug_name`
FROM
    `standard_case_outcome` AS `sco`
INNER JOIN
    `concept` AS `c1`
    ON
        `sco`.`outcome_concept_id` = `c1`.`concept_id`
LEFT JOIN
    `standard_case_drug` AS `scd`
    ON
        `sco`.`isr` = `scd`.`isr`
INNER JOIN
    `concept` AS `c2`
    ON
        `scd`.`outcome_concept_id` = `c2`.`concept_id`
INNER JOIN
    `standard_case_outcome_category` AS `scoc`
    ON
        `scoc`.`isr` = `sco`.`isr`
WHERE
    `scoc`.`outc_code` = 'CA'

修改

请注意,我遗漏了concept表,因为您没有从中选择任何内容,或者使用它过滤结果。

第二次编辑

已更新,以包含concept表格。更新后的问题表明,实际上需要SELECT

第三次编辑

需要分别为conceptname选择scoscd