php - 有没有将两列数据组合在一个数组中的解决方案?
问题描述
我有一个问题,我想使用数组将数据库中的Firstname和Lastname列显示为一个数据。顺便说一句,我是数组代码的新手。我试图浏览任何类似的结果,但无法提出解决方案。任何对我有帮助的事情都会受到高度赞赏。谢谢。
这是我当前的代码:
// DB table to use
$table = 'tbl_employee';
// Table's primary key
$primaryKey = 'id_ref';
// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array(
array( 'db' => 'emp_id_no', 'dt' => 0 ),
//this is my problem!! I want to combine this two different column.
array( 'db' => 'emp_lname' . "" .'db' => 'emp_fname', 'dt' => 1 ),
array( 'db' => 'emp_dep', 'dt' => 2 ),
array( 'db' => 'emp_job_name', 'dt' => 3 ),
array( 'db' => 'emp_status', 'dt' => 4 ),
);
解决方案
无需使用数组进行连接emp_lname
,emp_fname
您可以使用 sql 查询,这样可以加快处理速度并提高代码的可理解性。
$fullName = SELECT
emp_fname,
emp_lname,
CONCAT(emp_fname, ' ', emp_lname) full_name
FROM
tbl_employee
ORDER BY
full_name;
我在这里尝试做的是,从表中获取emp_fname
andemp_lname
然后将其分配给一个名为fullName
.so 的变量,因此您可以在数组中使用此变量,而不是连接数组中的列。
您可以使用此链接对sql server中的 concat 进行额外参考: http ://www.sqlservertutorial.net/sql-server-string-functions/sql-server-concat-function/
希望这将帮助您解决您的问题。
推荐阅读
- python - 将张量转换为 numpy 数组时出现值错误
- excel - excel visual basic中是否有任何功能可以查找没有空白行的列范围?
- c++ - c ++是否可以在不基于其基类的派生类中创建构造函数?
- python - pytest:测试数据库约束
- angular - 如何在可重用表中使用 matSort?
- javascript - 在javascript中分离一个嵌套数组?
- php - 在服务提供商中添加邮件配置在 laravel 7.0 中不起作用,它给出了不支持的邮件传输错误
- javascript - 使用 Firebase Emulator Suite 进行测试时,如何在 Firebase Cloud Functions 中使用 Jest 模拟函数?
- javascript - 为动态对象插入代理 localStorage
- regression - 如何在Stata中按“今天”导出回归模型结果和标签?