开发者

Querying from multiple tables with different columns

开发者 https://www.devze.com 2023-03-27 12:34 出处:网络
I have data in two tables that I want to query from. Each table has a different number of columns, but they both have one column, vID, that I want to use for my query. If I was querying one table, I w

I have data in two tables that I want to query from. Each table has a different number of columns, but they both have one column, vID, that I want to use for my query. If I was querying one table, I would use this column in my WHERE clause:

"SELECT * FROM table1 WHERE vID = ?"

And then I would use that cursor for my listAdapter.

The problem is in the same list, I also want to show data from ano开发者_开发百科ther table:

"SELECT * FROM table2 WHERE vID = ?"

Both tables have day, month, and year values that I want to use in my adapter, and a name column that I want to use.

I have tried UNION, but that returns an error because both tables have a different number of columns.

What should my query look like? Thanks.


If the columns are indeed the same, you need to specify those in the select to use UNION

SELECT foo,biz,baz FROM table1 WHERE vID = ?
UNION
SELECT foo,biz,baz FROM table2 WHERE vID = ?

The error you're getting is because the two tables have different columns, you can still UNION them, but you have to specify how to align the two tables if their schema is not identical.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号