您好 我确定有这种操作的查询,但我似乎无法得到它 我有一张这样的桌子:
product_id | saved_by | deleted_by | accepted_by | published_by
-----------+----------+------------+-------------+-------------
1 | user1 | | user1 |
-----------+----------+------------+-------------+-------------
2 | | user2 | |
-----------+----------+------------+-------------+-------------
3 | user2 | | | user3
-----------+----------+------------+-------------+-------------
4 | | | user1 | user4
-----------+----------+------------+-------------+-------------
我正在尝试创建一个查询,可以明确选择这些字段中的所有用户,结果如下:
users
------
user1
user2
user3
user4
任何指针?
答案 0 :(得分:3)
首先想到的是UNION列:
SELECT t.users
FROM
(
SELECT saved_by AS users
FROM table
UNION
SELECT deleted_by
FROM table
UNION
SELECT accepted_by
FROM table
UNION
SELECT published_by
FROM table
) AS t;
http://postgresql.org/docs/9.0/interactive/sql-select.html
作为旁注,如果这些数据被标准化,那么以您想要的方式获取数据会容易得多。