MySQL 如何将CONCAT()函数应用到表的列,并将它们与其他表的列结合起来
我们可以将应用于MySQL列的CONCAT()函数的输出与另一个MySQL表的列结合使用。这可以通过MySQL连接来完成。
阅读更多:MySQL 教程
例子
例如,我们有两个表‘Student’和‘Remarks’。其中‘Student’表包含学生的id、姓名、姓氏、地址和科目等细节,‘Remarks’表包含有关学生的评论。现在,以下查询可以将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’连接在一起。