Merging views to act as a single table with united records
Question
I have a couple of tables that are laid out like this:
+------------------------------------+
+ user_id | action_id | action_level +
+------------------------------------+
+ id_user | action_id | action_level +
+------------------------------------+
How do I create a MySQL view (or perhaps somebody could suggest a better option) where these will act as a single table:
+------------------------------------+
+ user_id | action_id | action_level +
+------------------------------------+
instead of joining it, which makes it show up like this:
+---------------------------------------------------------------------------------+
+ user_id | id_user | a_action_id | b_action_id | a_action_level | b_action_level +
+---------------------------------------------------------------------------------+
That way when I run a search against the table, I can simply search like this:
SELECT * FROM my_view WHERE action_id = 2;
Solution
Use a union
CREATE VIEW `user_actions` AS
SELECT a.user_id, a.action_id, a.action_level
UNION ALL
SELECT b.id_user AS user_id, b.action_id, b.action_level
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow