0
I’m doing a query of a table that doesn’t have a variable id
, but I’d like to assign a id
to my result. The following example:
Sample basis:
CREATE DATABASE breaking;
use breaking;
CREATE TABLE characters (name VARCHAR(100), sex VARCHAR(100), age VARCHAR(100));
INSERT INTO characters (name,sex,age)
VALUES ("W. White","male",54);
INSERT INTO characters (name,sex,age)
VALUES ("Hank Schroder","male",51);
INSERT INTO characters (name,sex,age)
VALUES ("Gus Fring","male",50);
INSERT INTO characters (name,sex,age)
VALUES ("Skyler","female",44);
Query:
SELECT name
FROM breaking.characters
WHERE sex='male'
Returns:
+---------------+
| name |
+---------------+
| W. White |
| Hank Schroder |
| Gus Fring |
+---------------+
To create the variable of id
in the output, I tried:
SELECT 1 as id, name
FROM breaking.characters
WHERE sex='male'
Returning:
+----+---------------+
| id | name |
+----+---------------+
| 1 | W. White |
| 1 | Hank Schroder |
| 1 | Gus Fring |
+----+---------------+
The problem with this solution is that it assigned the same id to everyone. How do I get different ids? In particular, I would like the ids to be a sequence of number of even compliance with the result. In this case, I have three names as a result, so I would like ids=[1,2,3].
I tried still:
SELECT (1,2,3) as id, name
FROM breaking.characters
WHERE sex='male'
But it didn’t work either.
your solution is correct, but avoid posting only the answer code. If possible, add an explanation for the code. See instructions on: https://answall.com/help/how-to-answer
– Lucas