这是我的表格,我想要唯一的价值,但所有的记录都是该值。
Original Table output
---------------
Department EmployeeName EmployeeID Department EmployeeName EmployeeID
------------------------------------ ------------------------------
Accounts Raj 123 Accounts Raj 123
Accounts Alok 124 Alok 124
Accounts Amit 125 Amit 125
Sales Henry 263 Sales Henry 263
Sales Mark 454 Mark 454
Hr Lusi 342 Hr Lusi 342
Hr Rosy 637 Rosy 637
答案 0 :(得分:1)
尝试使用不同的查询来选择唯一值
点击此链接SQL/mysql - Select distinct/UNIQUE but return all columns?
答案 1 :(得分:0)
这是一个完整演示的解决方案。
SQL:
-- data
create table t1(Department char(20), EmployeeName char(20), EmployeeID int);
insert into t1 values
('Accounts', 'Raj', 123 ),
('Accounts', 'Alok', 124 ),
('Accounts', 'Amit', 125 ),
('Sales', 'Henry', 263 ),
('Sales', 'Mark', 454 ),
('Hr', 'Lusi', 342 ),
('Hr', 'Rosy', 637 );
select * from t1;
-- Query needed
SELECT
CASE WHEN @last_dep IS NULL OR @last_dep != Department
THEN (@last_dep:=Department)
WHEN @last_dep = Department
THEN ''
END AS Department,
EmployeeName,
EmployeeID
FROM t1 JOIN (SELECT @last_dep:=NULL) v;
输出:
mysql> select * from t1;
+------------+--------------+------------+
| Department | EmployeeName | EmployeeID |
+------------+--------------+------------+
| Accounts | Raj | 123 |
| Accounts | Alok | 124 |
| Accounts | Amit | 125 |
| Sales | Henry | 263 |
| Sales | Mark | 454 |
| Hr | Lusi | 342 |
| Hr | Rosy | 637 |
+------------+--------------+------------+
7 rows in set (0.00 sec)
mysql>
mysql> -- Query needed
mysql> SELECT
-> CASE WHEN @last_dep IS NULL OR @last_dep != Department
-> THEN (@last_dep:=Department)
-> WHEN @last_dep = Department
-> THEN ''
-> END AS Department,
-> EmployeeName,
-> EmployeeID
-> FROM t1 JOIN (SELECT @last_dep:=NULL) v;
+------------+--------------+------------+
| Department | EmployeeName | EmployeeID |
+------------+--------------+------------+
| Accounts | Raj | 123 |
| | Alok | 124 |
| | Amit | 125 |
| Sales | Henry | 263 |
| | Mark | 454 |
| Hr | Lusi | 342 |
| | Rosy | 637 |
+------------+--------------+------------+
7 rows in set (0.00 sec)
答案 2 :(得分:0)
更简单的方法,:格式化可以在应用程序级别完成。
select Department, GROUP_CONCAT(EmployeeName),GROUP_CONCAT(EmployeeID) from t1 group by Department;
答案 3 :(得分:0)
SELECT Department,EmployeeName,EmployeeID FROM table_name GROUP BY Department;