我试图从每个ID
的2个不同表格中获取三件事,即对我RegID
我的第一个查询是选择DISTINCT RegID from table_one
如果有多个AttendanceStatus =Absent
,则应选择AttendanceDate = Todays_date
和RegID
DISTINCT Subjects
的应从同一Standard
table_one
和table_two
RegID
选择手机号码,它应该回显然后重复第二个RegID
如果存在同样应该重复,直到最后DISTINCT
我正在获取AttendanceStatus
regid但是在同一个while语句中如果使用获取结果来获取主题和标准以及注册ID它只给我一个id,主题和标准并且我没有得到如何解决这个问题
我的编辑
现在在我的示例数据库中,我有两个条目表示RegID
中缺席,所以在我的第一个选择i vl删除重复项(如果存在)并选择那些 2 RegID
正常工作对于每个Standard
,我想在我的第二个查询中选择subject
和RegID
,而在while语句中,有两个RegID
它只给我一个我需要那些两个缺席的标准与各自不同的主题和标准一起出现
简单
Standard
通过删除重复Subjects
RegID's
和RegID
Standard
Subjects
,RegID
和AttendanceDate Standard Subjects RegID AttendanceStatus
2016-01-08 00:00:00 III BSc PCM PHY/CHEM PRACTICAL 1382043 Present
2016-01-08 00:00:00 III BSc PCM PHY/CHEM PRACTICAL 1382044 Present
2016-01-08 00:00:00 III BSc PCM PHY/CHEM PRACTICAL 1382045 Present
2016-01-08 00:00:00 III BSc PCM PHY/CHEM PRACTICAL 1382046 Absent
2016-01-08 00:00:00 III BSc PCM PHY/CHEM PRACTICAL 1382047 Absent
醇>
我的table_one数据库
$query="SELECT DISTINCT RegID FROM table_one WHERE AttendanceDate='2016-01-08 00:00:00' and AttendanceStatus='Absent'" ;
$data=mysqli_query($mysqli,$query)or die(mysqli_error());
if(mysqli_num_rows($data) > 0) {
while($row=mysqli_fetch_array($data)){
$StudentRegID= $row['RegID'];
$query="SELECT DISTINCT(CONCAT(Standard,Subjects)) AS standard_and_subject , RegID FROM table_one WHERE AttendanceDate='2016-01-08 00:00:00' and RegID='$StudentRegID'" ;
$data=mysqli_query($mysqli,$query)or die(mysqli_error());
if(mysqli_num_rows($data) > 0) {
while($row=mysqli_fetch_array($data)){
if($row['RegID'] != '' && $row['RegID'] != NULL){
$RegID = $row['RegID'];
$standard_and_subject = $row['standard_and_subject'];
echo $standard_and_subject;
echo $RegID;
}
}
}
}
}
PHP Mysqli代码
getAjax()
答案 0 :(得分:2)
使用JOIN
的单个查询,而不是foreach
循环内的查询。而不是在PHP中检查空RegID
,而是在SQL中过滤掉它们。
$query = "SELECT DISTINCT t1.RegID, CONCAT(t2.Standard, t2.Subjects) AS standard_and_subject
FROM (SELECT DISTINCT RegID
FROM table_one
WHERE AttendanceDate='2016-01-08 00:00:00'
AND AttendanceStatus='Absent'
AND RegID != ''
AND RegID IS NOT NULL) AS t1
JOIN table_one AS t2 ON t1.RegID = t2.RegID
WHERE t2.AttendanceData = '2016-01-08 00:00:00'";
$data = mysqli_query($query) or die(mysqli_error());
if (mysqli_num_rows($data) > 0) {
while ($row = mysqli_fetch_assoc($data)) {
echo $row['standard_and_subject'];
echo $row['RegID'];
}
}
答案 1 :(得分:0)
您的商店mysql对象导致相同的变量名称
尝试此编码
while($standard_row=mysqli_fetch_array($data)){
if($standard_row['RegID'] != '' && $standard_row['RegID'] != NULL){
$RegID = $standard_row['RegID'];
$standard_and_subject = $standard_row['standard_and_subject'];
echo $standard_and_subject;
echo $RegID;
}