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