如何将应用于表列的 MySQL CONCAT() 函数与其他表的列组合?

mysqlmysqli database

我们可以使用应用于 MySQL 列的 CONCAT() 函数的输出与另一个 MySQL 表的列组合。这可以借助 MySQL 连接来完成。

示例

例如,我们有两个表"Student",其中包含学生的 id、姓名、姓氏、地址和科目等详细信息,以及"Remarks",其中包含学生的 id 和评论。现在,以下查询可以将 CONCAT() 函数与另一个表列组合 −

mysql> Select * from remarks;
+------+-------------+
| ID   | Comment     |
+------+-------------+
| 1    | Good        |
| 2    | Excellent   |
| 15   | Average     |
| 20   | Good        |
| 21   | Outstanding |
+------+-------------+
5 rows in set (0.00 sec)

mysql> Select CONCAT(Name,' ' ,Last_Name ), Comment from student s, remarks r
-> Where s.id = r.id;
+------------------------------+-------------+
| CONCAT(Name,' ' ,Last_Name ) | Comment     |
+------------------------------+-------------+
| Gaurav Kumar                 | Good        |
| Aarav Sharma                 | Excellent   |
| Harshit Kumar                | Average     |
| Gaurav Rathore               | Good        |
| Yashraj Singh                | Outstanding |
+------------------------------+-------------+
5 rows in set (0.00 sec)

两张表是根据两张表中的学生的共同'id'进行连接的。


相关文章