android.database.sqlite.SQLiteException:“ UNION”附近:语法错误

时间:2019-01-04 13:05:45

标签: java android sqlite android-sqlite

我正在为Android使用SQLite数据库

SQLite查询

SELECT t1.id,t1.name, t1.email FROM ((SELECT id,first_name as 
name,email FROM single_general where unique_code='UD') 
UNION ALL (SELECT name,email FROM school where 
unique_code='UD') ) t1

2 个答案:

答案 0 :(得分:1)

SELECT syntax不允许在复合查询中加上括号。 删除它们,以便仅保留子查询周围的括号:

SELECT id, name, email
FROM (SELECT id, first_name AS name, email
      FROM single_general
      WHERE unique_code = 'UD'
      UNION ALL
      SELECT 0, name, email
      FROM school
      WHERE unique_code = 'UD');

答案 1 :(得分:0)

联合中所有查询中的列数必须相同。 “学校”表查询中没有ID列。您可以从“学校”获取ID字段吗?尝试以下代码在第二个查询中为ID创建一个0填充字段:

SELECT t1.id,t1.name, t1.email FROM ((SELECT id,first_name as 
name,email FROM single_general where unique_code='UD') 
UNION ALL (SELECT 0 as id,name,email FROM `school` where 
unique_code='UD') ) t1