使用MySQL GROUP BY从特定公司获取员工姓名?
让我们首先创建一个表-
mysql> create table DemoTable642( EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,EmployeeName archar(100), EmployeeCompanyNamevarchar(100) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable642(EmployeeName,EmployeeCompanyName) values('John','Google'); mysql> insert into DemoTable642(EmployeeName,EmployeeCompanyName) values('Carol','Microsoft'); mysql> insert into DemoTable642(EmployeeName,EmployeeCompanyName) values('John','Amazon'); mysql> insert into DemoTable642(EmployeeName,EmployeeCompanyName) values('Carol','Google'); mysql> insert into DemoTable642(EmployeeName,EmployeeCompanyName) values('John','Flipkart');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable642;
这将产生以下输出-
+------------+--------------+---------------------+ | EmployeeId | EmployeeName | EmployeeCompanyName | +------------+--------------+---------------------+ | 1 | John | Google | | 2 | Carol | Microsoft | | 3 | John | Amazon | | 4 | Carol | Google | | 5 | John | Flipkart | +------------+--------------+---------------------+ 5 rows in set (0.00 sec)
以下是从特定公司获取员工姓名的查询-
mysql> select EmployeeName from DemoTable642 where EmployeeCompanyName="Google" group by EmployeeName;
这将产生以下输出-
+--------------+ | EmployeeName | +--------------+ | John | | Carol | +--------------+ 2 rows in set (0.00 sec)