Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SqliteDataReader and duplicate column names when using LEFT JOIN

Tags:

sqlite

Is there a documentation/specification about Sqlite3 that would describe would is supposed to happen in the following case?

Take this query:

var cmd = new SqliteCommand("SELECT Items.*, Files.* FROM Items LEFT JOIN Files ON Files.strColName = Items.strColName");

Both Items and Files have a column name "strColName". If an entry exists in Files, it will be joined to the result, if not, it will be NULL. Let's assume I always need the value of strColName, no matter if it is coming from Items or from Files. If I execute a reader:

var reader = cmd.ExecuteReader();

If there is a match in Files, reader["strColName"] will obviously contain the correct result because the value is set and it is the same in both tables. But if there wasn't a match in Files, will the NULL value of Files overwrite the non-NULL value of Items?

I'm really looking for some specification that defines how a Sqlite3 implementation has to deal with this case, so that I can trust either result.

like image 300
Krumelur Avatar asked Nov 26 '25 19:11

Krumelur


1 Answers

SQLite has no problem returning multiple columns labelled with the same name. However, the columns will always be returned in exactly the same order they are written in the SELECT statement. So, when you are searching for "strColName", you will find the first one, from Items.

It is recommended to use explicit column names instead of * so that the order is clear, and you can access values by their column index, if needed (and you detect incompatible changes in the table structure).

like image 180
CL. Avatar answered Nov 28 '25 15:11

CL.