【问题标题】:About showing datas through subquery关于通过子查询显示数据
【发布时间】:2017-10-04 08:52:21
【问题描述】:

我有这样的架构

http://sqlfiddle.com/#!3/690e8

  1. 我如何显示医生从未做过检查的医生编号、医生姓名、医生电话和医生出生年份(从医生出生日期开始)。

  2. 如何显示一年中第 12 个月销售的药品的药品名称、药品类型名称和药品价格。

  3. 我如何显示Medicineid 、medicinename 和medicineprice (with USD) where the drug is not sold by doctorid = 'dc001'。

  4. 如何在比患者年轻的医生为患者服务的地方显示患者 ID、患者姓名和患者出生年份(从患者出生日期开始)。

即使我是一名数字艺术专业的学生(这很复杂),我叔叔也让我解决这个问题,老实说,我对 mysql 几乎一无所知,只有基本的东西。拜托,我恳求你帮我解决这些问题。我会很感激的!

【问题讨论】:

  • 分享您为解决上述问题所做的一些代码/查询工作,而不仅仅是发布问题

标签: mysql terminal subquery


【解决方案1】:

1- 我如何显示医生从未做过检查的医生编号、医生姓名、医生电话和医生出生年份(从医生出生日期开始)。

select doctorid, doctorname, doctorphone , year(DoctorBirthDate) as doctorbirthyear
from msdoctor 
where doctorid not in (select doctorid from transactionheader) ; 

2- 如何显示一年中第 12 个月出售的药品的药名、药名和药价。

select medicinename, medicinetypename,  medicineprice 
from MsMedicineType mt, MsMedicine m , TransactionDetail td, TransactionHeader th
where mt.MedicineTypeID=m.MedicineTypeID and m.MedicineID=td.MedicineID and td.TransactionID=th.TransactionID and month(th.TransactionDate) = 12;

3- 我如何在医生 ID = 'dc001' 不出售的药物的情况下显示药物 ID、药物名称和药物价格(以美元计)。

select medicinename, medicinetypename,  concat(medicineprice,'$') as medicineprice 
from MsMedicineType mt, MsMedicine m , TransactionDetail td, TransactionHeader th
where mt.MedicineTypeID=m.MedicineTypeID and m.MedicineID=td.MedicineID and td.TransactionID=th.TransactionID and th.DoctorID!='dc001';

4- 如何在比患者年轻的医生为患者服务的地方显示患者 ID、患者姓名和患者出生年份(从患者出生日期开始)。

select p.patientid, p.patientname, year(PatientBirthDate) as patientbirthyear 
from TransactionHeader th , msdoctor m,  mspatient p 
where th.DoctorID=m.DoctorID and th.PatientID=p.PatientID and PatientBirthDate>DoctorBirthDate;

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    第一个问题

    SELECT DoctorID, DoctorName, DoctorPhone, YEAR(DoctorBirthDate) as 'DoctorBirthYear' 
    FROM MsDoctor
    WHERE DoctorID NOT IN (SELECT DoctorID FROM TransactionHeader);
    

    第二个问题

    SELECT MsMedicine.MedicineName, CONCAT(MsMedicine.MedicinePrice,' USD'), MsMedicineType.MedicineTypeName  
    FROM MsMedicine
    INNER JOIN MsMedicineType ON MsMedicineType.MedicineTypeID = MsMedicine.MedicineTypeID
    WHERE MedicineID IN (
        SELECT MedicineID FROM TransactionDetail
        WHERE TransactionID IN (
           SELECT TransactionID from TransactionHeader
            WHERE MONTH(TransactionDate) = 12
        )
    )
    

    【讨论】:

      猜你喜欢
      • 2018-07-09
      • 2021-08-17
      • 1970-01-01
      • 2017-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多