简体   繁体   English

MySQL子查询在3个表上返回多于1行

[英]MySQL Subquery returns more than 1 row on 3 tables

SELECT b.bill_no, b.case_no, b.patient_id,
(Select (lastname) from myhospital.patient p where p.patient_id = b.patient_id) as l_name,
(Select (givenname) from myhospital.patient p where p.patient_id = b.patient_id) as f_name,
(Select (middle) from myhospital.patient p where p.patient_id = b.patient_id) as m_name,
(select (address_street) from myhospital.patient p where
p.patient_id = b.patient_id) as adress, m. item_name,
(select cast(m.unit_price as Char(8))) as unit_price,
(select cast(m.qty as Char(8))) as quantity,
(select cast(m.charges as Char(8))) as charges,
m.date_rec, m.service_code,
(select (descript) from myhospital.hosp_services s where m.service_code = s.service_code) as Section,
(Select (fullname) from myhospital.users u where u.user_id = m.edit_by) as Encode_by,
(Select (descript) from myhospital.hosp_bill_etc c where b.bill_no = c.bill_no) as misc_edit
FROM myhospital.hosp_bill b join myhospital.hosp_bill_meds m
where b.bill_no = m.bill_no

I have join 2 tables from 1 database and i want to add another table which is "myhospital.hosp_bill_etc" and i am getting an error 我从1个数据库中加入了2个表,我想添加另一个表“ myhospital.hosp_bill_etc”,但出现错误

subquery returns more than 1 row, 子查询返回1行以上,

please someone tell me how to solve this. 请有人告诉我如何解决这个问题。

As you stated, you are obviously new to querying, and it does take practice. 如您所述,您显然是新手,它确实需要练习。 Start by learning the relationships between the tables, and to direct joins (or left joins) without doing repeated queries. 首先,了解表之间的关系,并直接进行联接(或左联接),而不进行重复查询。 So, the patient information should be a single record for the given "patient_id". 因此,患者信息应为给定“ Patient_id”的单个记录。 The joins between tables needs to identify HOW they are related or you will get Cartesian results. 表之间的联接需要确定它们之间的联系方式,否则您将获得笛卡尔结果。 Notice how I am showing the relationship between respective tables via the "ON" command. 注意如何通过“ ON”命令显示各个表之间的关系。 And for readability, notice how I am visually nesting the table relations such as from billing to bill meds to hosp services, and users etc. 为了提高可读性,请注意我如何直观地嵌套表格关系,例如从计费到账单药品到医院服务以及用户等。

Now, you can get any column from the respective table(s) in the field selection list by the simple alias... Anyhow, hopefully a little help for you... Also, I don't know why you are casting the charges, qty, price as character. 现在,您可以通过简单的别名从字段选择列表中的各个表中获取任何列...不管怎样,希望对您有所帮助...而且,我也不知道您为什么要收费,数量,价格为字符。 Typically output to what ever would formatted there and leave original value(s) as-is. 通常输出到在那里将要格式化的内容,并保持原始值不变。

SELECT 
      b.bill_no, 
      b.case_no, 
      b.patient_id,
      p.lastname as l_name,
      p.givenname as f_name,
      p.middle as m_name,
      p.address_street as address, 
      m. item_name,
      m.unit_price,
      m.qty as quantity,
      m.charges,
      m.date_rec, 
      m.service_code,
      s.descript as Section,
      u.fullname as Encode_by,
      c.descript as misc_edit
   FROM 
      myhospital.hosp_bill b 
         JOIN myhospital.patient p
            ON b.patient_id = p.patient_id

         JOIN myhospital.hosp_bill_meds m
            ON b.bill_no = m.bill_no
            JOIN myhospital.hosp_services s 
               ON m.service_code = s.service_code
            JOIN myhospital.users u 
               ON m.edit_by = u.user_id

         JOIN myhospital.hosp_bill_etc c 
            ON b.bill_no = c.bill_no

So this is the relationships for the tables, but it will now return ALL entries for ALL patients. 因此,这是表的关系,但是现在它将返回所有患者的所有条目。 If you want something for a specific bill, or patient, you would add a WHERE clause for that specific component. 如果您想要某些用于特定账单或患者的东西,则可以为该特定组件添加WHERE子句。

Now, it appears a bill is always to a single patient. 现在,看来账单总是给一个病人。 A bill has many meds. 账单上有很多药。 Each med I would think has a single service, but if one med can have more than one, you will get duplicates. 我认为每种医学都有一项服务,但是如果一种医学可以提供多种服务,那么您将获得重复的医学服务。 Also, for each med, I would expect a single person associated with who recorded/distributed the meds. 另外,对于每种药物,我希望有一个与记录/分发药物的人相关联。

Finally your "bill_etc". 最后是您的“ bill_etc”。 If this has multiple rows, that too could cause a Cartesian result. 如果它有多行,那也可能导致笛卡尔结果。

Hopefully a good start based on YOUR data environment vs so many generics that you might have to wrap your head around, but PLEASE do some more reading on SQL practices. 希望基于您的数据环境和如此之多的泛型,这是一个好的开始,您可能不得不全神贯注,但是请更多地阅读有关SQL实践的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM