表1名称:tbl_Goods
ID GName GStand GInPrice GOutPrice GBaseClass
91 长白山 1*10 10 50 烟
92 红双喜 5*7 40 60 烟
93 老白干 1*7 57 77 酒
-------------------------------------------------------------------
表2名称:tbl_SellBill
ID GID b_Date b_OPerson b_Time
11 91 2011-07-25 管理员 2011-7-25 22:32:56
-------------------------------------------------------------------
其中表2的GID就是表1中的ID
现在想要通过SQL语句提取出如下的表格内容:
表3名称:tbl_Result
ID GID GName GStand GInPrice GOutPrice GBaseClass b_Date b_OPerson b_Time
99 91 长白山 1*10 10 50 烟 2011-07-25 管理员 2011-7-25 22:32:56
=======================================================================================================
想要一个实现上述内容的表格的SQL语句应该如何写呢?
最好能解释一下含义。
以上三个表的名称都列出,并且每个内容的第一行即是每个字段的名称,
有解答满意者,百分送上。
9 个解决方案
#1
1.你的那个表3,其实就是查询结果,因此用不着名称,如果一定要名称,可以采用视图来处理.
2.查询结果中的ID 99 ,疑似 11.
2.查询结果中的ID 99 ,疑似 11.
#2
CREATE TABLE tbl_Goods(ID int,GName nvarchar(10),GStand varchar(10),GInPrice int,GOutPrice int,GBaseClass nvarchar(10))
insert into tbl_Goods select 91,'长白山','1*10',10,50,'烟'
insert into tbl_Goods select 92,'红双喜','5*7',40,60,'烟'
insert into tbl_Goods select 93,'老白干','1*7',57,77,'酒'
create table tbl_SellBill(ID int,GID int,b_Date varchar(10),b_OPerson varchar(10),b_Time datetime)
insert into tbl_SellBill select 11,91,'2011-07-25','管理员','2011-7-25 22:32:56'
go
select b.ID,b.GID,a.GName,a.GStand,a.GInPrice,a.GOutPrice,a.GBaseClass,b.b_Date,b.b_OPerson,b.b_Time
from tbl_Goods a inner join tbl_SellBill b on a.id=b.Gid
/*
ID GID GName GStand GInPrice GOutPrice GBaseClass b_Date b_OPerson b_Time
----------- ----------- ---------- ---------- ----------- ----------- ---------- ---------- ---------- -----------------------
11 91 长白山 1*10 10 50 烟 2011-07-25 管理员 2011-07-25 22:32:56.000
(1 行受影响)
*/
go
drop table tbl_Goods,tbl_SellBill
#3
如果用视图,则:
CREATE TABLE tbl_Goods(ID int,GName nvarchar(10),GStand varchar(10),GInPrice int,GOutPrice int,GBaseClass nvarchar(10))
insert into tbl_Goods select 91,'长白山','1*10',10,50,'烟'
insert into tbl_Goods select 92,'红双喜','5*7',40,60,'烟'
insert into tbl_Goods select 93,'老白干','1*7',57,77,'酒'
create table tbl_SellBill(ID int,GID int,b_Date varchar(10),b_OPerson varchar(10),b_Time datetime)
insert into tbl_SellBill select 11,91,'2011-07-25','管理员','2011-7-25 22:32:56'
go
create view tbl_Result
as
select b.ID,b.GID,a.GName,a.GStand,a.GInPrice,a.GOutPrice,a.GBaseClass,b.b_Date,b.b_OPerson,b.b_Time
from tbl_Goods a inner join tbl_SellBill b on a.id=b.Gid
go
select * from tbl_Result
/*
ID GID GName GStand GInPrice GOutPrice GBaseClass b_Date b_OPerson b_Time
----------- ----------- ---------- ---------- ----------- ----------- ---------- ---------- ---------- -----------------------
11 91 长白山 1*10 10 50 烟 2011-07-25 管理员 2011-07-25 22:32:56.000
(1 行受影响)
*/
go
#4
create table tbl_Goods(ID int, GName varchar(8), GStand varchar(8), GInPrice int,GOutPrice int,GBaseClass varchar(4))
insert tbl_Goods
select 91 ,'长白山','1*10', 10 ,50 ,'烟' union all
select 92 ,'红双喜','5*7', 40 ,60 ,'烟' union all
select 93 ,'老白干','1*7', 57 ,77 ,'酒'
create table tbl_SellBill(ID int,GID int,b_Date datetime,b_OPerson varchar(8),b_Time datetime)
insert tbl_SellBill
select 11 ,91 ,'2011-07-25','管理员','2011-7-25 22:32:56'
select a.ID,GID,GName,GStand,GInPrice,GOutPrice,GBaseClass,b_Date,b_OPerson,b_Time
from tbl_Goods a,tbl_SellBill b
where a.id=b.gid
#5
create table tbl_Goods(ID int, GName varchar(8), GStand varchar(8), GInPrice int,GOutPrice int,GBaseClass varchar(4))
insert tbl_Goods
select 91 ,'长白山','1*10', 10 ,50 ,'烟' union all
select 92 ,'红双喜','5*7', 40 ,60 ,'烟' union all
select 93 ,'老白干','1*7', 57 ,77 ,'酒'
create table tbl_SellBill(ID int,GID int,b_Date datetime,b_OPerson varchar(8),b_Time datetime)
insert tbl_SellBill
select 11 ,91 ,'2011-07-25','管理员','2011-7-25 22:32:56'
select b.ID,GID,GName,GStand,GInPrice,GOutPrice,GBaseClass,b_Date,b_OPerson,b_Time
from tbl_Goods a,tbl_SellBill b
where a.id=b.gid
/*
/*
ID GID GName GStand GInPrice GOutPrice GBaseClass b_Date b_OPerson b_Time
----------- ----------- ---------- ---------- ----------- ----------- ---------- ---------- ---------- -----------------------
11 91 长白山 1*10 10 50 烟 2011-07-25 管理员 2011-07-25 22:32:56.000
(1 行受影响)
*/
#6
如果一定要创建一个新表,则可以用 into 语句:
CREATE TABLE tbl_Goods(ID int,GName nvarchar(10),GStand varchar(10),GInPrice int,GOutPrice int,GBaseClass nvarchar(10))
insert into tbl_Goods select 91,'长白山','1*10',10,50,'烟'
insert into tbl_Goods select 92,'红双喜','5*7',40,60,'烟'
insert into tbl_Goods select 93,'老白干','1*7',57,77,'酒'
create table tbl_SellBill(ID int,GID int,b_Date varchar(10),b_OPerson varchar(10),b_Time datetime)
insert into tbl_SellBill select 11,91,'2011-07-25','管理员','2011-7-25 22:32:56'
go
select b.ID,b.GID,a.GName,a.GStand,a.GInPrice,a.GOutPrice,a.GBaseClass,b.b_Date,b.b_OPerson,b.b_Time into tbl_Result
from tbl_Goods a inner join tbl_SellBill b on a.id=b.Gid
go
select * from tbl_Result
/*
ID GID GName GStand GInPrice GOutPrice GBaseClass b_Date b_OPerson b_Time
----------- ----------- ---------- ---------- ----------- ----------- ---------- ---------- ---------- -----------------------
11 91 长白山 1*10 10 50 烟 2011-07-25 管理员 2011-07-25 22:32:56.000
(1 行受影响)
*/
go
#7
--用select更出要显示的字段
select b.ID,GID,GName,GStand,GInPrice,GOutPrice,GBaseClass,b_Date,b_OPerson,b_Time
--来自给tbl_Goods表和tbl_SellBill表,给tbl_Goods表设置一个别名a,给tbl_SellBill表设置一个别名b
from tbl_Goods a,tbl_SellBill b
--两个表关联的条件是 a.id=b.gid
where a.id=b.gid
#8
--用select更出要显示的字段
select b.ID,GID,GName,GStand,GInPrice,GOutPrice,GBaseClass,b_Date,b_OPerson,b_Time
--把新建一个tbl_Result表,把查询的结果存入此表
into tbl_Result
--来自给tbl_Goods表和tbl_SellBill表,给tbl_Goods表设置一个别名a,给tbl_SellBill表设置一个别名b
from tbl_Goods a,tbl_SellBill b
--两个表关联的条件是 a.id=b.gid
where a.id=b.gid
#9
这个在查询出的结果表3里的GName和表1(tbl_Goods)的GName是一样的吧?
就是说如果按你命名的表的话,
表a和表b,查询出的结果:
a.gname和b.gname是一样的吧?
我说明白了么?
#1
1.你的那个表3,其实就是查询结果,因此用不着名称,如果一定要名称,可以采用视图来处理.
2.查询结果中的ID 99 ,疑似 11.
2.查询结果中的ID 99 ,疑似 11.
#2
CREATE TABLE tbl_Goods(ID int,GName nvarchar(10),GStand varchar(10),GInPrice int,GOutPrice int,GBaseClass nvarchar(10))
insert into tbl_Goods select 91,'长白山','1*10',10,50,'烟'
insert into tbl_Goods select 92,'红双喜','5*7',40,60,'烟'
insert into tbl_Goods select 93,'老白干','1*7',57,77,'酒'
create table tbl_SellBill(ID int,GID int,b_Date varchar(10),b_OPerson varchar(10),b_Time datetime)
insert into tbl_SellBill select 11,91,'2011-07-25','管理员','2011-7-25 22:32:56'
go
select b.ID,b.GID,a.GName,a.GStand,a.GInPrice,a.GOutPrice,a.GBaseClass,b.b_Date,b.b_OPerson,b.b_Time
from tbl_Goods a inner join tbl_SellBill b on a.id=b.Gid
/*
ID GID GName GStand GInPrice GOutPrice GBaseClass b_Date b_OPerson b_Time
----------- ----------- ---------- ---------- ----------- ----------- ---------- ---------- ---------- -----------------------
11 91 长白山 1*10 10 50 烟 2011-07-25 管理员 2011-07-25 22:32:56.000
(1 行受影响)
*/
go
drop table tbl_Goods,tbl_SellBill
#3
如果用视图,则:
CREATE TABLE tbl_Goods(ID int,GName nvarchar(10),GStand varchar(10),GInPrice int,GOutPrice int,GBaseClass nvarchar(10))
insert into tbl_Goods select 91,'长白山','1*10',10,50,'烟'
insert into tbl_Goods select 92,'红双喜','5*7',40,60,'烟'
insert into tbl_Goods select 93,'老白干','1*7',57,77,'酒'
create table tbl_SellBill(ID int,GID int,b_Date varchar(10),b_OPerson varchar(10),b_Time datetime)
insert into tbl_SellBill select 11,91,'2011-07-25','管理员','2011-7-25 22:32:56'
go
create view tbl_Result
as
select b.ID,b.GID,a.GName,a.GStand,a.GInPrice,a.GOutPrice,a.GBaseClass,b.b_Date,b.b_OPerson,b.b_Time
from tbl_Goods a inner join tbl_SellBill b on a.id=b.Gid
go
select * from tbl_Result
/*
ID GID GName GStand GInPrice GOutPrice GBaseClass b_Date b_OPerson b_Time
----------- ----------- ---------- ---------- ----------- ----------- ---------- ---------- ---------- -----------------------
11 91 长白山 1*10 10 50 烟 2011-07-25 管理员 2011-07-25 22:32:56.000
(1 行受影响)
*/
go
#4
create table tbl_Goods(ID int, GName varchar(8), GStand varchar(8), GInPrice int,GOutPrice int,GBaseClass varchar(4))
insert tbl_Goods
select 91 ,'长白山','1*10', 10 ,50 ,'烟' union all
select 92 ,'红双喜','5*7', 40 ,60 ,'烟' union all
select 93 ,'老白干','1*7', 57 ,77 ,'酒'
create table tbl_SellBill(ID int,GID int,b_Date datetime,b_OPerson varchar(8),b_Time datetime)
insert tbl_SellBill
select 11 ,91 ,'2011-07-25','管理员','2011-7-25 22:32:56'
select a.ID,GID,GName,GStand,GInPrice,GOutPrice,GBaseClass,b_Date,b_OPerson,b_Time
from tbl_Goods a,tbl_SellBill b
where a.id=b.gid
#5
create table tbl_Goods(ID int, GName varchar(8), GStand varchar(8), GInPrice int,GOutPrice int,GBaseClass varchar(4))
insert tbl_Goods
select 91 ,'长白山','1*10', 10 ,50 ,'烟' union all
select 92 ,'红双喜','5*7', 40 ,60 ,'烟' union all
select 93 ,'老白干','1*7', 57 ,77 ,'酒'
create table tbl_SellBill(ID int,GID int,b_Date datetime,b_OPerson varchar(8),b_Time datetime)
insert tbl_SellBill
select 11 ,91 ,'2011-07-25','管理员','2011-7-25 22:32:56'
select b.ID,GID,GName,GStand,GInPrice,GOutPrice,GBaseClass,b_Date,b_OPerson,b_Time
from tbl_Goods a,tbl_SellBill b
where a.id=b.gid
/*
/*
ID GID GName GStand GInPrice GOutPrice GBaseClass b_Date b_OPerson b_Time
----------- ----------- ---------- ---------- ----------- ----------- ---------- ---------- ---------- -----------------------
11 91 长白山 1*10 10 50 烟 2011-07-25 管理员 2011-07-25 22:32:56.000
(1 行受影响)
*/
#6
如果一定要创建一个新表,则可以用 into 语句:
CREATE TABLE tbl_Goods(ID int,GName nvarchar(10),GStand varchar(10),GInPrice int,GOutPrice int,GBaseClass nvarchar(10))
insert into tbl_Goods select 91,'长白山','1*10',10,50,'烟'
insert into tbl_Goods select 92,'红双喜','5*7',40,60,'烟'
insert into tbl_Goods select 93,'老白干','1*7',57,77,'酒'
create table tbl_SellBill(ID int,GID int,b_Date varchar(10),b_OPerson varchar(10),b_Time datetime)
insert into tbl_SellBill select 11,91,'2011-07-25','管理员','2011-7-25 22:32:56'
go
select b.ID,b.GID,a.GName,a.GStand,a.GInPrice,a.GOutPrice,a.GBaseClass,b.b_Date,b.b_OPerson,b.b_Time into tbl_Result
from tbl_Goods a inner join tbl_SellBill b on a.id=b.Gid
go
select * from tbl_Result
/*
ID GID GName GStand GInPrice GOutPrice GBaseClass b_Date b_OPerson b_Time
----------- ----------- ---------- ---------- ----------- ----------- ---------- ---------- ---------- -----------------------
11 91 长白山 1*10 10 50 烟 2011-07-25 管理员 2011-07-25 22:32:56.000
(1 行受影响)
*/
go
#7
--用select更出要显示的字段
select b.ID,GID,GName,GStand,GInPrice,GOutPrice,GBaseClass,b_Date,b_OPerson,b_Time
--来自给tbl_Goods表和tbl_SellBill表,给tbl_Goods表设置一个别名a,给tbl_SellBill表设置一个别名b
from tbl_Goods a,tbl_SellBill b
--两个表关联的条件是 a.id=b.gid
where a.id=b.gid
#8
--用select更出要显示的字段
select b.ID,GID,GName,GStand,GInPrice,GOutPrice,GBaseClass,b_Date,b_OPerson,b_Time
--把新建一个tbl_Result表,把查询的结果存入此表
into tbl_Result
--来自给tbl_Goods表和tbl_SellBill表,给tbl_Goods表设置一个别名a,给tbl_SellBill表设置一个别名b
from tbl_Goods a,tbl_SellBill b
--两个表关联的条件是 a.id=b.gid
where a.id=b.gid
#9
这个在查询出的结果表3里的GName和表1(tbl_Goods)的GName是一样的吧?
就是说如果按你命名的表的话,
表a和表b,查询出的结果:
a.gname和b.gname是一样的吧?
我说明白了么?