【问题标题】:Not getting (SELECT) all the results using where in MySql Database未使用 MySql 数据库中的 where 获取(选择)所有结果
【发布时间】:2014-07-28 16:08:27
【问题描述】:

昨晚我有一个问题,以确保我已正确格式化我的 php,以确保我能够测试查询。今天在 mysql workbench 工作时,我发现我无法得到我想要的所有结果。我的“联系人”表中目前有 15 行,但是当我运行以下代码时,只有 8 行通过。请注意,我正在使用多个表格,但其中一些表格每个联系人的行数不止一行,而在同一个表格中,有些表格中的某些表格没有行,而其他表格则有多个表格。

 SELECT 
`Contact`.`firstName`,
`Contact`.`lastName`,
`ssn`.`ssn`,
`Contact`.`country`,
`Allergies`.`allergy`,
`Allergies`.`allergyType`,
`Allergies_Contact`.`allergyNotes`,
`CurrentPrescriptions`.`prescriptionName`,
`CurrentPrescriptions`.`prescribedDate`,
`BloodType`.`bloodType`
FROM
`mher`.`Contact`,
`mher`.`Allergies_Contact`,
`mher`.`Allergies`,
`mher`.`ssn`,
`mher`.`CurrentPrescriptions`,
`mher`.`BloodType`
WHERE
`Contact`.`contactKey` = `Allergies_Contact`.`contactKey`
    AND `Allergies`.`allergiesKey` = `Allergies_Contact`.`allergiesKey`
    AND `ssn`.`contactKey` = `Contact`.`contactKey`
    AND `CurrentPrescriptions`.`contactKey` = `Contact`.`contactKey`
    AND `BloodType`.`contactKey` = `Contact`.`contactKey`;

【问题讨论】:

  • 我们有机会看到你的表结构吗?
  • @JonH 这些是默认的 MySQL 转义字符,大多数数据库使用 [],mysql 使用 ``
  • 将 db 结构添加到 main
  • 重新阅读您的问题后,听起来您想要某些表格没有结果的条目,这是正确的,如果是,哪些表格?
  • 这是正确的,过敏表(Allergies 和 Allergy_Contact)和处方表

标签: mysql select where mysql-workbench


【解决方案1】:

你能试试吗,我已经在你不需要条目的表上将它作为左连接:

  SELECT 
     `Contact`.`firstName`,
     `Contact`.`lastName`,
     `ssn`.`ssn`,
     `Contact`.`country`,
     `Allergies`.`allergy`,
     `Allergies`.`allergyType`,
     `Allergies_Contact`.`allergyNotes`,
     `CurrentPrescriptions`.`prescriptionName`,
     `CurrentPrescriptions`.`prescribedDate`,
     `BloodType`.`bloodType`
  FROM
     `mher`.`Contact`
     INNER JOIN `mher`.`ssn`
        ON `ssn`.`contactKey` = `Contact`.`contactKey`
     INNER JOIN `mher`.`BloodType`
        ON `BloodType`.`contactKey` = `Contact`.`contactKey`
     LEFT JOIN `mher`.`Allergies_Contact`
        ON `Contact`.`contactKey` = `Allergies_Contact`.`contactKey`
     LEFT JOIN `mher`.`Allergies`
        ON `Allergies`.`allergiesKey` = `Allergies_Contact`.`allergiesKey`
     LEFT JOIN `mher`.`CurrentPrescriptions`
        ON `CurrentPrescriptions`.`contactKey` = `Contact`.`contactKey`
  ;

【讨论】:

  • 有效,您能解释一下为什么解决方案不起作用以及您的解决方案为何起作用
  • 当然,您正在执行所谓的合格交叉连接,这与我在上面替换它的显式连接完全相同,但有一个很大的例外,使用 LEFT JOIN 表示如果匹配的行不存在,而是将我加入一个全空行(INNER JOINWHERE 条件将丢弃该行)。
  • 这是有道理的,我知道我的论点中还缺少其他内容,但是阅读 mysql 查询的文档时我很难弄清楚如何引入空行
【解决方案2】:

或者 - 在我看来,更具可读性...

SELECT c.firstName
     , c.lastName
     , ssn.ssn
     , c.country
     , a.allergy
     , a.allergyType,
     , ac.allergyNotes
     , pc.prescriptionName
     , pc.prescribedDate
     , bc.bloodType
  FROM Contact c
  LEFT
  JOIN Allergies_Contact ac
    ON ac.contactKey = c.contactKey 
  LEFT
  JOIN Allergies a
    ON a.allergiesKey = ac.allergiesKey
  JOIN ssn
    ON ssn.contactKey = c.contactKey
  LEFT
  JOIN CurrentPrescriptions
    ON pc.contactKey = c.contactKey
  JOIN BloodType
   AND bc.contactKey = c.contactKey;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-29
    • 1970-01-01
    相关资源
    最近更新 更多