I have three tables with same-named columns, I want to select users distinctly from these all tables (no repeat) here is my query and it works,
SELECT User FROM Table1 UNION SELECT User FROM Table2 UNION SELECT User FROM Table3
How do I add another column to Select
something like this,
SELECT User, Time
But I only need to union on User column, Time column just for view purpose only.
Update: Here are the 3 tables I have. I want to get user names from these tables without repeating (distinct). and I want the time column to show in result
User | Time
Sam | 12.00
Jack | 12.00
Harry | 12.00
John | 12.00
John | 12.00
Sam | 12.00
User | Time
Jacob | 12.00
Jack | 12.00
Harry | 12.00
John | 12.00
Charlie | 12.00
Sam | 12.00
User | Time
Mason | 12.00
William | 12.00
Harry | 12.00
John | 12.00
Charlie | 12.00
Sam | 12.00
The result should be like this:
User | Time
Mason | 12.00
William | 12.00
Harry | 12.00
John | 12.00
Charlie | 12.00
Sam | 12.00
Jacob | 12.00
Jack | 12.00
Since it sounds like your actual data, unlike your sample, has different timestamps for each user in the different tables, and you don't care which of those timestamps is picked as long as the user names are unique...
SELECT User, min(Time)
FROM (SELECT User, Time FROM Table1
UNION ALL
SELECT User, Time FROM Table2
UNION ALL
SELECT User, Time FROM Table3)
GROUP BY User;
Your sample data implies that the Time
value is always the same, and therefore you don't mind picking any value in the table. One option here would be to just select a constant value as the Time
value:
SELECT User, '12.00' AS Time FROM Table1 UNION ALL
SELECT User, '12.00' FROM Table2 UNION ALL
SELECT User, '12.00' FROM Table3
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.