请教高手,此SQL语句该如何写?(100分)

  • 主题发起人 主题发起人 d99
  • 开始时间 开始时间
D

d99

Unregistered / Unconfirmed
GUEST, unregistred user!
表1: Employee<br>----------------------------<br> &nbsp; &nbsp; ID &nbsp; &nbsp; &nbsp;Name<br> &nbsp; &nbsp; E1 &nbsp; &nbsp; &nbsp;张三<br> &nbsp; &nbsp; E2 &nbsp; &nbsp; &nbsp;林耐<br> &nbsp; &nbsp; E3 &nbsp; &nbsp; &nbsp;何倍<br>----------------------------<br>表2: Product<br> &nbsp; &nbsp; ID &nbsp; &nbsp; Name<br> &nbsp; &nbsp; P1 &nbsp; &nbsp; 空调<br> &nbsp; &nbsp; P2 &nbsp; &nbsp; 风扇<br> &nbsp; &nbsp; P3 &nbsp; &nbsp; 电视<br>----------------------------<br>表3: Pright<br> &nbsp; &nbsp; EID &nbsp; &nbsp; PID<br> &nbsp; &nbsp; E1 &nbsp; &nbsp; &nbsp;P1<br> &nbsp; &nbsp; E1 &nbsp; &nbsp; &nbsp;P2<br> &nbsp; &nbsp; E1 &nbsp; &nbsp; &nbsp;P3<br> &nbsp; &nbsp; E2 &nbsp; &nbsp; &nbsp;P1<br> &nbsp; &nbsp; E2 &nbsp; &nbsp; &nbsp;P2<br> &nbsp; &nbsp; E2 &nbsp; &nbsp; &nbsp;P3<br> &nbsp; &nbsp; E3 &nbsp; &nbsp; &nbsp;P1<br> &nbsp; &nbsp; E3 &nbsp; &nbsp; &nbsp;P2<br> &nbsp; &nbsp; E3 &nbsp; &nbsp; &nbsp;P3<br>---------------------------<br>根据表1、表2生成表3的SQL语句。
 
insert into Pright(EID,PID) select A.ID As EID,B.ID As PID From Employee A,Product B
 
SELECT A.ID EID, B.ID PID<br> &nbsp;Employee A, Product B
 
left join?路过。
 
declare @t1 table(aa int)<br>insert @t1 select 1<br>union select 2<br><br>declare @t2 table(bb int)<br>insert @t2 select 3<br>union select 4<br><br>select A.aa,B.bb from @t1 A,@t2 B order by A.aa<br><br>select A.aa,B.bb from @t1 A cross join @t2 B order by A.aa<br><br>上面两个select结果一样<br>用其他的join也可以,只要加上on 1=1 这样的条件就ok了。
 
谢谢各位!<br>如果改变成如下:(将delected=1的记录忽略)<br><br>表1: Employee<br>----------------------------<br> &nbsp; &nbsp; ID &nbsp; &nbsp; &nbsp;Name &nbsp; &nbsp; &nbsp;delected<br> &nbsp; &nbsp; E1 &nbsp; &nbsp; &nbsp;张三 &nbsp; &nbsp; &nbsp; &nbsp; 1<br> &nbsp; &nbsp; E2 &nbsp; &nbsp; &nbsp;林耐 &nbsp; &nbsp; &nbsp; &nbsp; 0<br> &nbsp; &nbsp; E3 &nbsp; &nbsp; &nbsp;何倍 &nbsp; &nbsp; &nbsp; &nbsp; 0<br>----------------------------<br>表2: Product<br> &nbsp; &nbsp; ID &nbsp; &nbsp; Name &nbsp; &nbsp; &nbsp; delected<br> &nbsp; &nbsp; P1 &nbsp; &nbsp; 空调 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0<br> &nbsp; &nbsp; P2 &nbsp; &nbsp; 风扇 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1<br> &nbsp; &nbsp; P3 &nbsp; &nbsp; 电视 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0<br>----------------------------<br>表3: Pright<br> &nbsp; &nbsp; EID &nbsp; &nbsp; PID<br> &nbsp; &nbsp; E2 &nbsp; &nbsp; &nbsp;P1<br> &nbsp; &nbsp; E2 &nbsp; &nbsp; &nbsp;P3<br> &nbsp; &nbsp; E3 &nbsp; &nbsp; &nbsp;P1<br> &nbsp; &nbsp; E3 &nbsp; &nbsp; &nbsp;P3<br>---------------------------<br>该如何写呢?谢谢!
 
SELECT A.ID EID, B.ID PID<br> &nbsp;Employee A, Product B<br> WHERE A.delected = 0<br> &nbsp; AND B.delected = 0
 
通过表1与表2生成表3???<br> &nbsp;Select Employee.id AS EID,Product.ID AS PID into Pright From Employee Inner Join Product &nbsp;on Employee.id = Producd.ID<br> &nbsp;这样可以通过E表与P表自动生成一个PRIGHT表
 
select Pright(EID,PID) select A.ID As EID,B.ID As PID From (Select * From Employee Where delected = 0) A,(Select * From Product Where delected = 0) B
 
select employee.id as EID,Product.id as PID from employee left join product<br>where employee.delected=0 and product.delected=0
 
Select Employee.id AS EID,Product.ID AS PID into Pright From Employee Inner Join Product &nbsp;on 1 = 1<br>Where Employee Deleted &lt;&gt; 1<br> &nbsp;And Product &nbsp;&lt;&gt; 1
 
后退
顶部