SQL - 从主表中查找链接到另一个表

时间:2017-05-15 12:46:45

标签: c# sql

我有3个表,PatientDoctor(链接患者表主键和医生表主键),患者和医生

在我的医生表中,

----------------------------------
|ID|Doctor Name|Specialty|Year Ex|
----------------------------------
|1 |Alex       |Transplt | 3     |
----------------------------------

在我的病人医生,

-------------------------------
|ID|Patient Name|Ward|Diseases|
-------------------------------
|5 |Berns       |1234| Cancer |
-------------------------------

在我的PatientDoctor中,

------------------------
|ID|Patient ID|DoctorID|
------------------------
|6 |5         |1       |
------------------------

现在,我想搜索与患者相关的医生,就像我从ComboBox搜索“Alex”一样,listBox将显示所有患者相关。这是我目前使用的代码,但仍然会返回错误。

> if (comboBox1.SelectedIndex==-1)
>                 MessageBox.Show("Nothing to search!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
>             else
>             {
>                 using (SqlConnection connection = new SqlConnection(connectionString))
>                 {
>                     //string sql = "SELECT * FROM Patient " + "WHERE [Patient Name] = @name";
>                     string sql = "SELECT a.[Patient Name] FROM Patient a " + "INNER JOIN PatientDoctor b ON a.ID = b.[DoctorID] " + "INNER
> JOIN Doctor c ON b.ID = c.[Doctor Name]" + "WHERE c.[Doctor Name] =
> @DID";
>                     using (SqlCommand cmd = new SqlCommand(sql, connection))
>                     {
>                         cmd.Parameters.AddWithValue("@DID", comboBox1.SelectedValue.ToString());
> 
>                         DataTable dt = new DataTable();
>                         SqlDataAdapter ad = new SqlDataAdapter(cmd);
>                         ad.Fill(dt);
> 
>                         if (dt.Rows.Count > 0)
>                         { //check if the query returns any data
>                             listBox1.DataSource = dt;
>                             //dg1.DataBind();
>                         }
>                         else
>                         {
>                             MessageBox.Show("Record not found!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
>                         }
>                         textBox1.Text = "";
>                     }
>                 }
>             }

3 个答案:

答案 0 :(得分:0)

您在这里错过了一个空格c.[Doctor Name]" + "WHERE c.[Doctor Name] = ...它应该是c.[Doctor Name]" + " WHERE c.[Doctor Name] =

您的查询应该是

string sql = "SELECT a.[Patient Name] FROM Patient a INNER JOIN PatientDoctor b ON a.ID = b.[DoctorID] INNER JOIN Doctor c ON b.ID = c.[Doctor Name] WHERE c.[Doctor Name] = @DID";

答案 1 :(得分:0)

你的逻辑错了。您正在加入id的名称。我想你打算:

SELECT p.[Patient Name]
FROM Patient p INNER JOIN 
     PatientDoctor pd
     ON p.ID = pd.PatientID INNER JOIN
     Doctor d
     ON pd.DoctorId = d.Id
WHERE pd.DoctorName = @DID;

注意:

  • @DID重命名为@DoctorName。您希望按名称搜索而不是ID。
  • 患者姓名的联接使用DoctorId,这是荒谬的。
  • 使用表名称缩写的表别名,以便查询更容易理解。
  • 不要依赖AddWithValue()。使用明确正确的类型添加参数。

答案 2 :(得分:0)

请尝试从c#开始:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication55
{
    class Program
    {

        static void Main(string[] args)
        {
            DataTable doctors = new DataTable();
            doctors.Columns.Add("ID", typeof(int));
            doctors.Columns.Add("Doctor Name", typeof(string));
            doctors.Columns.Add("Speciality", typeof(string));
            doctors.Columns.Add("Year Ex", typeof(int));

            doctors.Rows.Add(new object[] { 1, "Alex", "Transplt", 3});

            DataTable patients = new DataTable();
            patients.Columns.Add("ID", typeof(int));
            patients.Columns.Add("Patient Name", typeof(string));
            patients.Columns.Add("Ward", typeof(int));
            patients.Columns.Add("Diseases", typeof(string));

            patients.Rows.Add(new object[] { 5, "Berns", 1234, "Cancer"});

            DataTable patientDoctor = new DataTable();
            patientDoctor.Columns.Add("ID", typeof(int));
            patientDoctor.Columns.Add("Patient ID", typeof(int));
            patientDoctor.Columns.Add("DoctorID", typeof(int));

            patientDoctor.Rows.Add(new object[] { 6,5,1});

            string doctorName = "Alex";
            int doctID = doctors.AsEnumerable().Where(x => x.Field<string>("Doctor Name") == doctorName).Select(x => x.Field<int>("ID")).FirstOrDefault();

            var results = (from patDr in patientDoctor.AsEnumerable()
                           join pat in patients.AsEnumerable() on patDr.Field<int>("Patient ID") equals pat.Field<int>("ID")
                           select new { doctorID = patDr.Field<int>("DoctorID"), patient = pat.Field<string>("Patient Name") })
                           .Where(x => x.doctorID == doctID).Select(x => x.patient).ToList();

        }


    }

}