I have the following table called pr_collaborator
:
--------------------------------
id | name | last_name |
--------------------------------
000015| John | Smith |
002154| Maria | Sanchez |
123456| Fabian | Sierra |
SE0012| Sarah | Taylor |
SE0015| Conny | Huertas |
--------------------------------
I just made this query:
SELECT * FROM pr_collaborator;
And this is the result:
--------------------------------
id | name | last_name |
--------------------------------
15 | John | Smith |
2154| Maria | Sanchez |
123456| Fabian | Sierra |
SE0012| Sarah | Taylor |
SE0015| Conny | Huertas |
--------------------------------
I want to keep the leading zeros, the id
field is VARCHAR(10)
I was trying the following but it does not work;
SELECT CAST(id as CHAR) AS id, name, last_name FROM pr_collaborator;
SELECT CONVERT(id, VARCHAR) AS id, name, last_name FROM pr_collaborator;
I don't want to use LPAD
function because I don't know how many characters have the values.
Update
This is the pr_collaborator
structure:
CREATE TABLE `pr_collaborator` (
`id` varchar(10) COLLATE utf8_spanish_ci NOT NULL,
`name` varchar(80) CHARACTER SET latin1 NOT NULL,
`last_name` varchar(80) CHARACTER SET latin1 NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci
This is the query in PHP code:
$sql = "SELECT id, name, last_name FROM pr_collaborator";
$result = $this->db->fetchAll($sql, Phalcon\DB::FETCH_ASSOC);
$this->jsonReturnSuccess($result);
Maybe it could be a problem with PHP and no with my MySQL
I hope that you can help me!.
I cannot replicate this finding:
INSERT INTO my_table VALUES (8,NOW(),'00010102');
Query OK, 1 row affected (0.03 sec)
SELECT status FROM my_table WHERE uuid = 8;
+----------+
| status |
+----------+
| 00010102 |
+----------+
I don't know if it is a good answer, but I had to do this:
I had to put quotes in the column value using QUOTE()
function because PHP becomes my values as a integers:
$sql = "SELECT QUOTE(id) AS id, name, last_name FROM pr_collaborator";
$result = $this->db->fetchAll($sql, Phalcon\DB::FETCH_ASSOC);
$this->jsonReturnSuccess($result);
As you can see I can keep leading zeros in the query result because of quotes.
array(
'id' : '000015',
'name': 'Jhon',
'last_name': 'Smith'
),
array(
'id' : '002154',
'name': 'Maria',
'last_name': 'Sanchez'
),
array(
'id' : '123456',
'name': 'Fabian',
'last_name': 'Sierra'
),
array(
'id' : 'SE0012',
'name': 'Sarah',
'last_name': 'Taylor'
),
array(
'id' : 'SE0015',
'name': 'Conny',
'last_name': 'Huertas'
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.