I'm currently using an API through Azure to connect to my app. I'm running a query which populates the list view for the user's medications. The user has the choice whether to add a diagnosis along with their medication, or not. If the user adds a medication and diagnosis, the query is successful and will populate the database and list view. But if the user does not add a diagnosis, the database is updated successfully and the userdiagnosis
field is blank, but it will not show up on the list view on the app.
Is there any way to allow a null value to run in the query and still pull the data successfully?
This is my current query:
var api = {
// an example of executing a SQL statement directly
get: (request, response, next) => {
var query = {
sql: 'SELECT dbo.usermedication.id, \
dbo.medicine.id AS medicationid, \
dbo.medicine.medname, \
dbo.usermedication.isactive, \
dbo.usermedication.reminder, \
dbo.usermedication.communityfeedback, \
dbo.usermedication.frequency, \
dbo.usermedication.directions, \
dbo.medicine.dosage, \
dbo.usermedication.datetimetotake, \
dbo.userdiagnosis.id as "userdiagnosisid", \
dbo.diagnosis.id AS diagnosisid, \
dbo.diagnosis.diagnosistext \
FROM dbo.usermedication
INNER JOIN dbo.medicine ON dbo.usermedication.medicationid = dbo.medicine.id
INNER JOIN dbo.userdiagnosis ON dbo.usermedication.userdiagnosisid = dbo.userdiagnosis.id
INNER JOIN dbo.diagnosis ON dbo.userdiagnosis.diagnosisid = dbo.diagnosis.id
WHERE dbo.usermedication.userid = @userid AND dbo.usermedication.isactive = 1',
parameters: [
{ name: 'userid', value: request.query.userid }
]
};
request.azureMobile.data.execute(query)
.then(function (results) {
response.json(results);
});
}
};
module.exports = api;
答案 0 :(得分:0)
据您从给定的问题陈述中了解到,如果用户未添加诊断,则dbo.diagnosis表将没有条目,但dbo.usermedication将成功更新。 由于您在这些表上使用了INNER JOIN,因此显然列表将没有,因为dbo.diagnosis表中没有匹配的记录。
如果您考虑在dbo.diagnosis上使用LEFT JOIN,那么即使诊断表中的值为空,您也可以从userMedication表中获取记录。
如果这是期望的,请尝试。
也许您可以尝试运行以下内容
WITH (SELECT * FROM dbo.userdiagnosis
INNER JOIN dbo.diagnosis ON dbo.userdiagnosis.diagnosisid = dbo.diagnosis.id ) AS diagnosisDetails,
SELECT dbo.usermedication.id, \
dbo.medicine.id AS medicationid, \
dbo.medicine.medname, \
dbo.usermedication.isactive, \
dbo.usermedication.reminder, \
dbo.usermedication.communityfeedback, \
dbo.usermedication.frequency, \
dbo.usermedication.directions, \
dbo.medicine.dosage, \
dbo.usermedication.datetimetotake, \
DiagnosisDetails.id as "userdiagnosisid", \
DiagnosisDetails.diagnosistext \
FROM dbo.usermedication
INNER JOIN dbo.medicine ON dbo.usermedication.medicationid =
dbo.medicine.id
LEFT JOIN diagnosisDetails ON dbo.usermedication.userdiagnosisid = diagnosisDetails.id
WHERE dbo.usermedication.userid = @userid AND dbo.usermedication.isactive = 1;