Q:

Error in Sqlite : sqlite3_prepare_v2 failure: near \\\"UNION\\\" : syntax error\},\"code : 5} in IONIC 2

I am using sqlite in IONIC 2 to fetch data from chat table but it keeps giving me error at UNION.
Here is my query:
SELECT a.id, a.timeDate, a.message, fromUser, toUser FROM ((SELECT id, message, fromUser, toUser, toUser AS uid, timeDate FROM chat WHERE fromUser = ?) UNION (SELECT id, message, fromUser, toUser, fromUser AS uid, timeDate FROM chat WHERE toUser = ? )) AS a GROUP BY a.uid ORDER BY a.id DESC , a.uid DESC

I tried with brackets before UNION and without bracket like
(query1) UNION (query2) and (query1 UNION query2) but still that’ not working.
This query is working fine in mysql with same structured table.
How to solve this problem?

A:

Subqueries use parentheses, but a compound query is not made up from subqueries, so it must not use parentheses.
In your query, only the subquery in the FROM clause uses parentheses:
SELECT ... FROM (SELECT ... UNION SELECT ...) AS a GROUP BY ...

