简体   繁体   中英

PostgreSQL UNION, but where do the results come from?

I want to do a union with two tables, but I need to know where the results are coming from. Here is my join.

(SELECT  * FROM watchlist_movies WHERE movie_id = $1 and user_id = $2)
UNION ALL
(SELECT* FROM favorite_movies WHERE movie_id = $1 and user_id = $2)

so my goal is return a single array with two objects in them. For example,

[
{watchList: [... results]},
{favorites: [... results]}
]

Is this result possible or would have to make two separate sql statements and pack them in a array in Node.js?

Try:

(SELECT 'Watchlist', a.* FROM watchlist_movies a WHERE movie_id = $1 and user_id = $2)
UNION ALL
(SELECT 'Favorites', b.* FROM favorite_movies b WHERE movie_id = $1 and user_id = $2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM