我正在尝试使用union来使用同一个表加入两个mysqli查询。如果我在phpmyadmin上运行sql但如果我在php脚本下运行它无法工作,它工作正常。我做错了什么?
<?php
$query = $db->query ("Select(select *, GREATEST(MAX(totadyo1), MAX(totadyo2), MAX(totadyo3), MAX(totadyo4), MAX(totadyo5)) as HIGH, MIN(totadyo1), MIN(totadyo2),
MIN(totadyo3), MIN(totadyo4), MIN(totadyo5) as LOW from adult_rpt a LEFT JOIN oldgroup2 ON a.oldgroup_id = oldgroup2.oldgroup_id
LEFT JOIN group2 ON a.group_id = group2.group_id LEFT JOIN district2 ON a.district_id = district2.district_id where service = 'sws'
and month = 'jan' group by district_name)
UNION
(select *, totadyo1,totadyo2,totadyo3,totadyo4,totadyo5, Case When totadyo1 < totadyo2 And totadyo1 < totadyo3
And totadyo1 < totadyo4 And totadyo1 < totadyo5 Then totadyo1 When totadyo2 < totadyo1 And totadyo2 < totadyo3 And
totadyo2 < totadyo4 And totadyo2 < totadyo5 Then totadyo2 When totadyo3 < totadyo1 And totadyo3 < totadyo2 And
totadyo3 < totadyo4 And totadyo3 < totadyo5 Then totadyo3 When totadyo4 < totadyo1 And totadyo4 < totadyo2 And
totadyo4 < totadyo3 And totadyo4 < totadyo5 Then totadyo4 Else totadyo5 End As LOW from adult_rpt a LEFT
JOIN oldgroup2 ON a.oldgroup_id = oldgroup2.oldgroup_id LEFT JOIN group2 ON a.group_id = group2.group_id LEFT
JOIN district2 ON a.district_id = district2.district_id where service = 'sws' and month = 'jan' group by district_name)")
or die(mysqli_error($query));
答案 0 :(得分:0)
感谢您的所有评论,尤其是Joshkisb。在再次查看代码之后,我将两个查询合并在一起并删除了union和bravo!它工作正常。
<?php
$query = $db->query("select *, GREATEST(MAX(totadyo1), MAX(totadyo2), MAX(totadyo3), MAX(totadyo4), MAX(totadyo5)) as HIGH,
totadyo1,totadyo2,totadyo3,totadyo4,totadyo5, Case When totadyo1 < totadyo2 And totadyo1 < totadyo3
And totadyo1 < totadyo4 And totadyo1 < totadyo5 Then totadyo1 When totadyo2 < totadyo1 And totadyo2 < totadyo3 And
totadyo2 < totadyo4 And totadyo2 < totadyo5 Then totadyo2 When totadyo3 < totadyo1 And totadyo3 < totadyo2 And
totadyo3 < totadyo4 And totadyo3 < totadyo5 Then totadyo3 When totadyo4 < totadyo1 And totadyo4 < totadyo2 And
totadyo4 < totadyo3 And totadyo4 < totadyo5 Then totadyo4 Else totadyo5 End As LOW from adult_rpt a
LEFT JOIN oldgroup2 ON a.oldgroup_id = oldgroup2.oldgroup_id
LEFT JOIN group2 ON a.group_id = group2.group_id
LEFT JOIN district2 ON a.district_id = district2.district_id where teacher_id='$session_id' and service = 'sws' and month = 'jan' group by district_name
ORDER BY a.report_id DESC") or die(mysqli_error());