SQL高手请进、谢谢! 有四张表,客户表,客户订单表,订单表,book表 .....写在问题补充里 在数据库中利用SQL语言建立“客户表”、“货品表”和“订单表...

作者&投稿:习肥 (若有异议请与网页底部的电邮联系)
问题1:
select address, client_name, phone, order_id
from client A
inner join client_order B
on A.client_id =B.client_id --这个不用解释吧
问题2:
select tt1.client_name, sum(tt2.count*tt2.price) as total_price
from (select client_name, order_id
from client A
inner join client_order B
on A.client_id =B.client_id) tt1 --得到客户订单表
inner join (select order_id,count,price
from order C
inner join book D
on C.book_id=D.book_id) tt2 --得到订单价格表
on tt1. order_id=tt2.order_id --根据订单号做连接
group by tt1.client_name --根据客户名汇总金额


有两种方法,效率上没有任何区别.
1, Oracle的传统多表查询
SELECT c.CourseName, s.StartDate, i.FirstName
|| ' ' ||i.LastName as Instructor, l.City
FROM ScheduledClasses s, Instructors i, Courses c, Locations l
WHERE s.InstructorID = i.InstructorID
AND s.CourseNumber = c.CourseNumber
AND s.LocationId = l.LocationID
AND l.Country = ‘USA'

2, ANSI标准查询(Oracle 9i以上支持)
SELECT c.CourseName, s.StartDate, i.FirstName
|| ' ' ||i.LastName as Instructor, l.City
FROM Instructors i
JOIN ScheduledClasses s ON (i.InstructorID = s.InstructorID)
JOIN Courses c ON (s.CourseNumber = c.CourseNumber)
JOIN Locations l ON (s.LocationId = l.LocationID)
WHERE l.Country = ‘USA'

补充下:如果要求每张订单可以包含多种图书,应该如何修改Order表的主键?为保证每张订单只被一个客户拥有,应该在ClientOrder表上增加怎么样的约束(提示:使用索引)

SQL高手请进,SQL语句查询试题~

表结构都不贴上来怎么帮你啊
难道是需要自己设计?

Create table #Custom
([CustomID] nvarchar(3),[Name] nvarchar(20))
Insert #Custom
select N'001','john' union all
select N'002','cathy' union all
select N'003','Alice' union all
select N'004','jack'

Create table #Product
([ProductId] nvarchar(3),[ProductName] nvarchar(20))
Insert #Product
select N'001','产品1' union all
select N'002','产品2' union all
select N'003','产品3' union all
select N'004','产品4'

Create table #Sales
([SalseID] nvarchar(3),[SalseName] nvarchar(20),[city] nvarchar(20))
Insert #Sales
select N'001','销售商1','beijing' union all
select N'002','销售商2','xian'

Create table #Order
([OrderID] nvarchar(3),[ProductId] nvarchar(3),
[CustomId] nvarchar(3),[SalesId] nvarchar(3))
Insert #Order
select N'001','001','001','001' union all
select N'002','002','001','001' union all
select N'003','001','002','002' union all
select N'004','003','003','002' union all
select N'005','004','002','002' union all
select N'006','002','004','001'


select count(*), productid from #ORder f
left join ((select customid,count(*) as countP from #Order
group by customid
having count(*)>=2)) a1 on a1.customId=f.customid
where countP>=2
group by productid
having count(*)>=2

select distinct f.customId,name from #ORder f
left join #sales a1 on a1.salseId=f.salesid
left join #custom a2 on a2.customid=f.customid
where a1.city='beijing'
--'beijing' 为输入的条件

CREATE TABLE
表名_客户表

列名1 类型 是否为空,
列名2 类型 是否为空,
列名3 类型 是否为空


例如:
Create table 客户表

姓名 char(16) NOT NULL,
电话 char(11) NOT NULL,
)

注意:在建表前先要选择数据库,