Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite column names without table name

When querying a database in SQLite I find it useful to give alias to the tables I am working with. It also helps resolve naming conflicts.

SELECT a._id, a.name, b.email FROM people AS a, emails AS b

However then when I look for the columns "_id", "name", and "email" in my cursor, I get an error that the columns cannot be found. It only works if I change the query to:

SELECT a._id AS "_id", a.name AS "name", b.email AS "email" FROM people AS a, emails AS b

Is there a way to get the base column name without the extra work?

like image 395
Samuel Avatar asked Jul 07 '26 20:07

Samuel


1 Answers

Declaring an alias to a column is a better way to avoid errors. The two tables the your querying may have the same column names and it can be difficult to differentiate them without aliases. The only way to differentiate them is by associating them with their table names or giving them aliases.

Aliases can make queries easier to both write and to read. You can give a table or a column another name by using an alias. This can be a good thing to do if you have very long or complex table names or column names

like image 142
reggie Avatar answered Jul 14 '26 13:07

reggie