SQL从结果数据库中选择subCode及其得分,并将其输出为一个字符串

[英]SQL to select subCode and its score from results database and output it as one string


I have a results table with the following description. I need a way to loop through the table and get subCode and grade of a certain admNo and put them together as one string in order to send it via SMS. The message content should look like this:

我有一个结果表,其中包含以下描述。我需要一种方法来遍历表并获得某个admNo的subCode和等级,并将它们组合成一个字符串,以便通过SMS发送它。消息内容应如下所示:

2129 CAT1 101 A- 102 C+ 121 A 231 C+. 

Contents of results table...

结果表的内容......

id  admNo   examtyp subCode  termId formId  streamId score  grade   points  year
1   2129    CAT1    101      1  3   0   87  A-  11  2013
1   2129    CAT1    102      1  3   0   65  C+  7   2013
1   2129    CAT1    121      1  3   0   90  A   12  2013
1   2129    CAT1    231      1  3   0   67  C+  7   2013
1   2129    CAT1    233      1  3   0   66  C+  7   2013
1   2129    CAT1    311      1  3   0   65  C+  7   2013
1   2129    CAT1    313      1  3   0   90  A   12  2013
1   2129    CAT1    565      1  3   0   60  C   6   2013
2   4093    CAT1    101      1  3   0   0   C+  7   2013
2   4093    CAT1    102      1  3   0   70  B-  8   2013

The above table stores subject scores of students. If there is a way I can loop through the array after fetching then that would work too.

上表存储了学生的科目分数。如果有一种方法可以在获取后循环遍历数组,那么这也可以。

2 个解决方案

#1


1  

If you want the message content to look like your example, you can do that with one row per admNo/examtyp:

如果您希望消息内容看起来像您的示例,则每个admNo / examtyp可以使用一行:

select concat(admNo, ' ', examtyp, ' ', 
              group_concat(concat(subcode, ' ', grade) separator ' '),
              '.'
             ) MessageLine
from Results r
group by admNo, examtyp

#2


0  

This PHP code does what you want:

这个PHP代码做你想要的:

$admNo = 4093;

$DB_NAME = "*dbname*";
$DB_HOST = "*hostname*";
$DB_USER = "*dbusername*";
$DB_PASS = "*dbpassword*";
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
$query = "SELECT * FROM results WHERE admNo=$admNo";
$result = $mysqli->query($query) or die($mysqli->error);
$total = $mysqli->affected_rows;
$linea = $result->fetch_assoc();
$string = $linea['admNo'].' '.$linea['examtyp'].' '.$linea['subCode'].' '.$linea['grade'].' ';
for($i = 0; $i < $total-1; $i++){
    $linea = $result->fetch_assoc();
    $string .= $linea['subCode'].' '.$linea['grade']. ' ';
}
print $string;

OUTPUT:

4093 CAT1 101 C+102 B-

This PHP code will populate the $string output with the format shown in the output, creating it from all entries for the admNo in the table. You can then do an SQL insert using the string to put it into the sms table as you need to

此PHP代码将使用输出中显示的格式填充$ string输出,并从表中admNo的所有条目创建它。然后,您可以使用该字符串执行SQL插入,以根据需要将其放入sms表中

This what you needed?

这是你需要的吗?


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/05/15/4f55fd99db92eebeaffdec172291abfa.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com