Both have a column called lastName and a column called indexID. I want the last names from both tables where the index id is the same in both tables. When I run the following SQL, I do not get the entry from the alternateNames table.
SELECT DISTINCT a.lastName as LastName FROM entries a WHERE a.indexID = ?
UNION
SELECT DISTINCT b.lastName as LastName FROM alternateNames b WHERE b.indexID = ?
ORDER BY LastName COLLATE NOCASE
If I remove one (it doesn’t matter which one) or both of the where clauses the sql works as expected.
Norman - Yes - unless the where clauses are removed as described.
Dave - that would work but I want a list of lastNames from both tables for a specific index id
Tim, no - I only pass the indexID once and I confirmed it’s the same value for both where clauses.
Norman, I will try that - thanks.
All, Maybe I wasn’t being clear. Maybe this will help.
Assume the entries table looks like this (other columns not shown):
lastName indexID
Hatfield 1
Hare 2
Gould 1
and AlternateNames looks like this (other columns not shown):
lastName IndexID
Palardy 1
Schneppmueller 2
The result of my UNION
SELECT a.lastName as LastName FROM entries a WHERE a.indexID = 1
UNION
SELECT b.lastName as LastName FROM alternateNames b WHERE b.indexID = 1
ORDER BY LastName COLLATE NOCASE
should be this:
Gould
Hatfield
Palardy
What I am getting is this (again, unless I remove one or both of the where clauses in the statement):
Gould
Hatfield
BTW, the statement works in the program ‘DB Browser for SQLite’
Norman was right - It had something to do with a prepared statement bug -
I changed this:
Dim stmt As PreparedSQLStatement = Self.Prepare(“SELECT a.lastName as LastName FROM entries a WHERE a.indexID = ? UNION SELECT b.lastName as LastName FROM alternateNames b WHERE b.indexID = ? ORDER BY LastName COLLATE NOCASE”)
to this:
Dim sql as String = “SELECT a.lastName as LastName FROM entries a WHERE a.indexID =” + Str(App.IndexID) + " UNION SELECT b.lastName as LastName FROM alternateNames b WHERE b.indexID = " + Str(App.IndexID) + " ORDER BY LastName COLLATE NOCASE"