如何在 mysql 中通过所需的列名设置 sql 结果索引值?例如
表名:国家
+-----------+----------------+--------------------+
| id | code | name |
+-----------+----------------+--------------------+
| 1 | 88 | Bangladesh |
+-----------+----------------+--------------------+
| 2 | 966 | Saudi Arabia |
+-----------+----------------+--------------------+
| 3 | 967 | Yemen |
+-----------+----------------+--------------------+
| 4 | 963 | Syria |
+-----------+----------------+--------------------+
| 5 | 249 | Sudan |
+-----------+----------------+--------------------+
创建表查询如下:
CREATE TABLE `country` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`code` int(20) NOT NULL,
`name` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
并将数据插入表如下:
insert into `country`(`id`,`code`,`name`) values (1,88,'Bangladesh'),
(2,966,'Saudi Arabia'),(3,967,'Yemen'),(4,963,'Syria'),(5,249,'Sudan');
结果将显示如下:
如果我执行以下查询和我想要的结果如下:
select code, name from country order by code ;
期望的结果:
[88] => Array
(
[code] => 88,
[name] => Bangladesh
)
[249] => Array
(
[code] => 249,
[name] => Sudan
)
[963] => Array
(
[code] => 963,
[name] => Syria
)
[966] => Array
(
[code] => 966,
[name] => Saudi Arabia
)
[967] => Array
(
[code] => 967,
[name] => Yemen
)
我需要的数组键将是直接 SQL 查询的代码。可能吗?