SELECT DISTINCT
response_user, patient_name, ipno, department
FROM
response
INNER JOIN
patient_details ON response.response_user = patient_details.id
INNER JOIN
key_points ON response.response_key = key_points.kp_id
WHERE
department = 'CVTS'
AND MONTH(response_on) BETWEEN '12' AND '12'
AND response_key BETWEEN 146 AND 149
AND response_val = '5' OR response_val = '4'
AND kp_belongs_to = '1'
ORDER BY
patient_name ASC
我想要特定部门的患者姓名
答案 0 :(得分:1)
我建议您使用表别名,并在列名之前使用这些别名,以便始终清楚哪个列属于哪个表。
尽管这是一个简单的SELECT
语句(没有花哨的地方),但是最好指定使用哪个数据库。
说存在“一些pblm”(键盘坏了吗?为什么不写“问题”呢?)却很难调试。运行该语句时是否有任何错误?如果是这样,哪一个?
此外,如果您格式化所编写的代码,那就太好了;当看起来更漂亮时,更容易发现罪魁祸首。我是这样做的,并且-如果其他一切正常,则表可以正确连接等, OR 可能是一个,即
SELECT DISTINCT response_user,
patient_name,
ipno,
department
FROM response
INNER JOIN patient_details ON response.response_user = patient_details.id
INNER JOIN key_points ON response.response_key = key_points.kp_id
WHERE department = 'CVTS'
AND month(response_on)BETWEEN '12' AND '12'
AND response_key BETWEEN 146 AND 149
AND ( response_val = '5' --> OR condition should be enclosed
OR response_val = '4' --> into parenthesis
)
AND kp_belongs_to = '1'
ORDER BY patient_name ASC
查看是否有帮助。如果没有,请考虑遵循我写的帮助我们的指南。