我的应用程序中有以下查询,
$que="SELECT DISTINCT prescription.commodity_name, prescription.dosage, prescription.duration, prescription.prescription_id, prescription.remarks, prescription.paid, employee.f_name, prescription.quantity_requested, employee.s_name, employee.other_name, prescription.doctor_id, request.strength, stock.unit_per_pack, stock.selling_price, stock.buying_price
FROM prescription
INNER JOIN request ON prescription.medname = request.commodity_name
INNER JOIN stock ON stock.stock_id = request.stock_id
INNER JOIN request ON request.request_id = transaction.transaction_id
INNER JOIN employee ON employee.employee_id = prescription.doctor_id
INNER JOIN visit ON prescription.visit_id = visit.visit_id
WHERE prescription.visit_id = '".$id."'";
当我运行脚本文件时,我收到以下数据错误:不唯一的表/别名:'request
我如何解决这个问题或者我做错了什么?
答案 0 :(得分:0)
你正在request
进行两次连接,这意味着,例如,当你SELECT request.strength
MySQL不知道要使用哪个表时。
考虑添加别名:
SELECT DISTINCT prescription.commodity_name, prescription.dosage, prescription.duration, prescription.prescription_id, prescription.remarks, prescription.paid, employee.f_name, prescription.quantity_requested, employee.s_name, employee.other_name, prescription.doctor_id,
r1.strength, -- Switched to use r1 alias
stock.unit_per_pack, stock.selling_price, stock.buying_price
FROM prescription
INNER JOIN request r1 ON prescription.medname = r1.commodity_name -- Using alias r1
INNER JOIN stock ON stock.stock_id = r1.stock_id
INNER JOIN request r2 ON r2.request_id = transaction.transaction_id -- Using alias r2
INNER JOIN employee ON employee.employee_id = prescription.doctor_id
INNER JOIN visit ON prescription.visit_id = visit.visit_id
WHERE prescription.visit_id = '".$id."'"
请注意,您可能需要调整上面使用的别名。
答案 1 :(得分:0)
您在两个联接中使用相同的别名“请求”。你需要给它一个独特的别名,这样做的一个例子是:
SELECT DISTINCT * FROM prescription a
INNER JOIN request b ON a.medname = b.commodity_name
INNER JOIN stock c ON c.stock_id = b.stock_id
INNER JOIN request d ...
此外,您的查询中是否有错误?您似乎正在尝试加入名为transaction
的表,但您没有在连接中定义它:
INNER JOIN request ON request.request_id = transaction.transaction_id
此请求表是否实际上是交易?:
INNER JOIN transaction ON request.request_id = transaction.transaction_id