我有三张桌子:预约,处方和药物。
约会表与处方和处方有关系与药物的关系。
我已经可以在药物表上获得药物类型的字段并显示在datagridview上。从约会表,跳到处方,然后跳到药物,以获取药物类型字段。
但是现在我想从约会表中再显示一个来自状态的字段,怎么做呢?
我收到了这个错误:
当EXISTS没有引入子查询时,只能在选择列表中指定一个表达式。
帮助。
我的表
'm'附近的语法不正确。
'ap'附近的语法不正确。
private void LoadPrescriptionRecords()
{
//retrieve connection information info from App.config
string strConnectionString = ConfigurationManager.ConnectionStrings["SACPConnection"].ConnectionString;
//STEP 1: Create connection
SqlConnection myConnect = new SqlConnection(strConnectionString);
//STEP 2: Create command
/*
string strCommandText = "SELECT prescriptionID, app.aDate, med.medicationName, pat.pFirstname, pay.amount FROM PRESCRIPTION AS pres";
strCommandText += " LEFT OUTER JOIN medication as med on pres.medicationid = med.medicationid";
strCommandText += " LEFT OUTER JOIN appointment as app on pres.appointmentid = app.appointmentid";
strCommandText += " LEFT OUTER JOIN patient as pat on pres.patienpaymentidtid = pat.patientid";
strCommandText += " LEFT OUTER JOIN payment as pay on pres. = pay.paymentid";
*/
/*
string strCommandText = "SELECT appointmentID FROM APPOINTMENT";
SqlCommand cmdAPPOINTMENT = new SqlCommand(strCommandText, myConnect);
string strCommandText2 = "SELECT medicationID FROM PRESCRIPTION WHERE appointmentID IN (" + strCommandText + ")";
SqlCommand cmdPRESCRIPTION = new SqlCommand(strCommandText2, myConnect);
*/
string strCommandText3 = "SELECT m.medicationType, ap.appointmentID,ap.aStatus MEDICATION m, (SELECT p.medicationID, a.appointmentID,a.aStatus from APPOINTMENT a, PRESCRIPTION p WHERE a.appointmentID = p.appointmentID) ap WHERE aa.medicationID = aa.medicationID";
//string strCommandText3 = "SELECT nFirstName FROM NURSE WHERE nurseID= (" + strCommandText2 + ")";
myConnect.Open();
/*
SqlDataReader readAPPOINTMENT = cmdAPPOINTMENT.ExecuteReader();
readAPPOINTMENT.Close();
SqlDataReader readPRESCRIPTION = cmdPRESCRIPTION.ExecuteReader();
readPRESCRIPTION.Close();
SqlDataReader readMEDICATION = cmdMEDICATION.ExecuteReader();
*/
PrescriptionAdapter = new SqlDataAdapter(strCommandText3, myConnect);
//readMEDICATION.Close();
myConnect.Close();
//command builder generates Select, update, delete and insert SQL
// statements for MedicalCentreAdapter
SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(PrescriptionAdapter);
// Empty Employee Table first
Prescription.Clear();
// Fill Employee Table with data retrieved by data adapter
// using SELECT statement
PrescriptionAdapter.Fill(Prescription);
// if there are records, bind to Grid view & display
if (Prescription.Rows.Count > 0)
grdPrescription.DataSource = Prescription;
}
答案 0 :(得分:1)
您有两种选择:
1)修改您的查询,以便允许一组值。示例:使用IN
代替=
2)修改子查询,使其只返回一行。示例:使用MAX()
函数将多行减少为一行。
答案 1 :(得分:1)
您可以执行单个查询以从多个字段中获取字段。请检查以下代码。请注意,此测试未经过测试可能需要进行一些更改。
string strCommandText3 = "SELECT m.medicationType,ap.appointmentID,ap.aStatus FROM MEDICATION as m, (SELECT p.medicationID, a.appointmentID,a.aStatus from
APPOINTMENT a, PRESCRIPTION p WHERE a.appointmentID = p.appointmentID) ap WHERE m.medicationID = ap.medicationID ";