找不到sql语法错误

时间:2014-07-06 21:50:38

标签: php mysql sql syntax

有人能发现我的语法错误吗?我是sql查询的新手。这段代码在我的项目的其他地方使用,我为我创建的新表重新格式化了但是我得到了以下错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM tbl_prescriptions ta, tbl_prescriptions_type tat, tbl_prescriptions_subtype' at line 1 QUERY:SELECT ta.pk_id, ta.date, tat.vch_type as vch_type_name, tar.vch_subtype as vch_subtype_name, concat(tas.vch_first_name, ' ', tas.vch_last_name) as vch_resource_name, FROM tbl_prescriptions ta, tbl_prescriptions_type tat, tbl_prescriptions_subtype tar, tbl_resources tas WHERE ta.fk_type_id = tat.pk_id AND ta.fk_resource_id = tas.pk_id AND ta.fk_subtype_id = tar.pk_id AND ta.fk_patient_id = 359 ORDER BY date DESC;

$sqlPrescriptionQuery = "SELECT ta.pk_id, ta.date, ";
$sqlPrescriptionQuery .= "tat.vch_type as vch_type_name, ";
$sqlPrescriptionQuery .= "tar.vch_subtype as vch_subtype_name, ";
$sqlPrescriptionQuery .= "concat(tas.vch_first_name, ' ', tas.vch_last_name) as vch_resource_name, ";
$sqlPrescriptionQuery .= "FROM tbl_prescriptions ta, tbl_prescriptions_type tat, tbl_prescriptions_subtype tar, tbl_resources tas ";
$sqlPrescriptionQuery .= "WHERE ";
$sqlPrescriptionQuery .= "ta.fk_type_id = tat.pk_id AND ";
$sqlPrescriptionQuery .= "ta.fk_resource_id = tas.pk_id AND ";
$sqlPrescriptionQuery .= "ta.fk_subtype_id = tar.pk_id AND ";
$sqlPrescriptionQuery .= "ta.fk_patient_id = ".$row['pk_id']." ";
$sqlPrescriptionQuery .= "ORDER BY date DESC;";

$counter = 0;

$prescriptionresult = mysql_query($sqlPrescriptionQuery) or die(mysql_error()."<br>QUERY:".$sqlPrescriptionQuery);

谢谢!

2 个答案:

答案 0 :(得分:1)

$sqlPrescriptionQuery = "
SELECT ta.pk_id
     , ta.date
     , tat.vch_type vch_type_name
     , tar.vch_subtype vch_subtype_name
     , CONCAT(tas.vch_first_name, ' ', tas.vch_last_name) vch_resource_name
  FROM tbl_prescriptions ta
     , tbl_prescriptions_type tat
     , tbl_prescriptions_subtype tar
     , tbl_resources tas 
 WHERE ta.fk_type_id = tat.pk_id 
   AND ta.fk_resource_id = tas.pk_id 
   AND ta.fk_subtype_id = tar.pk_id 
   AND ta.fk_patient_id = $row['pk_id']
 ORDER 
    BY date DESC;
    ";

答案 1 :(得分:0)

最后你的逗号太多了:

$sqlPrescriptionQuery .= "concat(tas.vch_first_name, ' ', tas.vch_last_name) as vch_resource_name, ";

应该是:

$sqlPrescriptionQuery .= "concat(tas.vch_first_name, ' ', tas.vch_last_name) as vch_resource_name ";