mysql—如何在sql中不使用联接和子查询而从三个表中获取数据

wa7juj8i  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(376)

我有三张table

emp(id, name)
product(id, productname)
sales(id,emp_id,product_id,saleprice)

列出所有员工的总销售额。
找到销售业绩最好的员工。
注意:我不想使用连接和子查询,请建议我更好的方式。

fsi0uk1n

fsi0uk1n1#

我想你可以这样做。但更好的方法是使用join语句。但根据您的要求,您可以这样做。我认为这是最简单的方法,也是非常简单的查询。
我提供了createtable和示例数据插入查询。
这是你要求的答案

-- 1.List all the employee with total sales.
select distinct E.Id,E.Name,P.Productname,S.saleprice from Sales S,Emp E,Product P
where E.Id=S.Emp_id and P.Id=S.Product_id

请看第一个问题的结果

-- 2.Fetch the employee with highest sales.
select S.Emp_id,E.Name,SUM(S.saleprice) AS TotalSalePrice from Sales S,Emp E
where E.Id=S.Emp_id 
Group By S.Emp_id,E.Name

请看第二个问题的结果
这是为您的需求创建的createtable和sampledatainsert查询

create table Emp
(
 [Id] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
 [Name] [nvarchar](100)
 );

create table Product(
 [Id] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
 [Productname] [nvarchar](100)
 );

create table Sales(
 [Id] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
 [Emp_id] [int] FOREIGN KEY REFERENCES Emp([Id]) ,
 [Product_id] [int] FOREIGN KEY REFERENCES Product([Id]) ,
 [saleprice] [int] NOT NULL 
 );

INSERT INTO [dbo].[Emp] ([Name]) VALUES('Jone Doe')
INSERT INTO [dbo].[Emp] ([Name]) VALUES('Micheal Oshea')
INSERT INTO [dbo].[Emp] ([Name]) VALUES('Ish Thalagala')
INSERT INTO [dbo].[Emp] ([Name]) VALUES('Mark Poull')
INSERT INTO [dbo].[Emp] ([Name]) VALUES('Janne Marker')

INSERT INTO [dbo].[Product]([Productname]) VALUES ('Coca Cola')
INSERT INTO [dbo].[Product]([Productname]) VALUES ('Pepsi')
INSERT INTO [dbo].[Product]([Productname]) VALUES ('Tooth Brush')
INSERT INTO [dbo].[Product]([Productname]) VALUES ('Water Filter')
INSERT INTO [dbo].[Product]([Productname]) VALUES ('Playstation 4 pro')

INSERT INTO [dbo].[Sales]([Emp_id],[Product_id],[saleprice])VALUES(1,1,10)
INSERT INTO [dbo].[Sales]([Emp_id],[Product_id],[saleprice])VALUES(1,4,500)

INSERT INTO [dbo].[Sales]([Emp_id],[Product_id],[saleprice])VALUES(2,2,10)
INSERT INTO [dbo].[Sales]([Emp_id],[Product_id],[saleprice])VALUES(2,5,600)
INSERT INTO [dbo].[Sales]([Emp_id],[Product_id],[saleprice])VALUES(2,4,500)

INSERT INTO [dbo].[Sales]([Emp_id],[Product_id],[saleprice])VALUES(3,1,10)
INSERT INTO [dbo].[Sales]([Emp_id],[Product_id],[saleprice])VALUES(3,2,10)
INSERT INTO [dbo].[Sales]([Emp_id],[Product_id],[saleprice])VALUES(3,3,30)
INSERT INTO [dbo].[Sales]([Emp_id],[Product_id],[saleprice])VALUES(3,4,500)
INSERT INTO [dbo].[Sales]([Emp_id],[Product_id],[saleprice])VALUES(3,5,600)

INSERT INTO [dbo].[Sales]([Emp_id],[Product_id],[saleprice])VALUES(4,1,10)

INSERT INTO [dbo].[Sales]([Emp_id],[Product_id],[saleprice])VALUES(5,4,500)
INSERT INTO [dbo].[Sales]([Emp_id],[Product_id],[saleprice])VALUES(5,5,600)
uemypmqf

uemypmqf2#

如果你只能参考emp\u id,那么就没有必要使用 JOIN 完全:

SELECT emp_id, SUM(salesprice) AS total_sales
FROM sales
GROUP BY emp_id
ORDER BY total_sales DESC  -- comment 2 lines to get answer for 1 question
LIMIT 1; -- MySQL version for SQL Server use `SELECT TOP 1`

请注意,如果特定员工没有任何销售,则将跳过。对于不存在的数据,将不会获得0/null值。
编辑:
如果你决定使用 JOIN 然后:

SELECT e.id, e.name, SUM(s.salesprice) AS total_sales
FROM emp e
LEFT JOIN sales s
  ON e.id = s.emp_id
GROUP BY e.id, e.name
ORDER BY BY total_sales DESC LIMIT 1;

相关问题