-1

Using UNION ALL with Queries with different number of column returns the following error sqlite3.OperationalError: SELECTs to the left and right of UNION ALL do not have the same number of result columns. I tried this answer but I think that is now outdated and does not work. I tried to find something in the documentation but I couldn't find it. Both UNION and UNION ALL do not work. This answer is a bit complex for me to understand.

What would be the workaround to achieve this? A Column with Null - how do I do that ?

Update:

Also, I don't know the no. or name of tables as in my program I allow the user to create and manipulate data. To find out the queries in the database, I Use:

SELECT name FROM sqlite_master WHERE type='table';

and to find out the columns I use this:

[i[0] for i in cursor.description]
Mayank
  • 1,595
  • 1
  • 11
  • 26

2 Answers2

0

Simple, the column size and type for each side of a union must be identical.

You can make them identical by casting columns to the correct type, or setting missing columns to NULL.

James Anderson
  • 27,109
  • 7
  • 50
  • 78
  • One Problem, I don't know the Name of Tables and Columns as in my program they are created and defined by the user. I updated the question. – Mayank Mar 20 '21 at 09:13
0

I think you just need to compensate for the missing columns with adding 'empty' columns.

CREATE TABLE test_table1(k INTEGER, v INTEGER);
CREATE TABLE test_table2(k INTEGER);

INSERT INTO test_table1(k,v) VALUES(4, 5);
INSERT INTO test_table2(k) VALUES(4);

SELECT * FROM test_table1 UNION ALL SELECT *,'N/A' FROM test_table2;
4|5
4|N/A

Here I've added another pseudo-column with 'N/A', so the test_table2 has two columns before the UNION ALL happens.

tvm
  • 3,263
  • 27
  • 37
  • One Problem, I don't know the Name of Tables and Columns as in my program they are created and defined by the user. I updated the question. – Mayank Mar 20 '21 at 09:13