本人在写SQL语句的时候遇到一些困难,可否帮忙解答,谢谢!数据库表的结构如下:
consignee 部门表
字段:
cgid,部门ID 如1001,1002
cgname部门名称 如点心,蔬菜 branch 分店表
字段:
ltdid,分店ID 如1032,1033
cgname 分店名称 如 aa,bb orderlist 订单表
字段:
listid 分店ID
branchname分店名称
cgid 部门ID
checkout 是否确认 1 确认,0未确认 我想问一下如何写SQL语句实现如下
分店 ,部门,状态 可以显示所有的分店与所有部门的订单状态,谢谢!!
consignee 部门表
字段:
cgid,部门ID 如1001,1002
cgname部门名称 如点心,蔬菜 branch 分店表
字段:
ltdid,分店ID 如1032,1033
cgname 分店名称 如 aa,bb orderlist 订单表
字段:
listid 分店ID
branchname分店名称
cgid 部门ID
checkout 是否确认 1 确认,0未确认 我想问一下如何写SQL语句实现如下
分店 ,部门,状态 可以显示所有的分店与所有部门的订单状态,谢谢!!
http://topic.csdn.net/u/20091130/21/fb718680-98ff-4afb-98d8-cff2f8293ed5.html?24281
那请问SQL语句应该怎样写啊!?
(
select m.ltdid , m.cgname 分店, n.cgid , n.cgname 部门 from branch m cross join consignee n
) t
left join orderlist p
on t.ltdid = p.listid and t.cgid = p.cgid
from
branch
left
join
(select distinct cgid,ltdid,branchname,ordertype,checkout from orderlist) orderlist
on
branch.ltdid=orderlist.ltdid
如果我这样写,就只显示所有分店并且没有所有部门!
(
select m.ltdid , m.cgname 分店, n.cgid , n.cgname 部门 from branch m cross join consignee n
) t
left join
orderlist p
on t.ltdid= p.listidand t.cgid= p.cgid --或者
select t.* , isnull(cast(p.checkout as varchar),'没有下单') 状态 from
(
select m.ltdid , m.cgname 分店, n.cgid , n.cgname 部门 from branch m cross join consignee n
) t
left join
orderlist p
on t.ltdid= p.listidand t.cgid= p.cgid
(
select m.ltdid , m.cgname 分店, n.cgid , n.cgname 部门 from branch m cross join consignee n
) t
left join
orderlist p
on t.ltdid= p.listid and t.cgid= p.cgid --或者
select t.* , isnull(cast(p.checkout as varchar),'没有下单') 状态 from
(
select m.ltdid , m.cgname 分店, n.cgid , n.cgname 部门 from branch m cross join consignee n
) t
left join
orderlist p
on t.ltdid= p.listid and t.cgid= p.cgid
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
CREATE TABLE [dbo].[branch] (
[ltdid] [int] NOT NULL ,
[branchname] [varchar] (30) COLLATE Chinese_PRC_CI_AS NOT NULL ,
) ON [PRIMARY]
GOCREATE TABLE [dbo].[consignee] (
[cid] [int] IDENTITY (1, 1) NOT NULL ,
[cgid] [int] NOT NULL ,
[cgname] [varchar] (20) COLLATE Chinese_PRC_CI_AS NOT NULL ,
) ON [PRIMARY]
GOCREATE TABLE [dbo].[orderlist] (
[listid] [int] IDENTITY (1, 1) NOT NULL ,
[ltdid] [int] NULL ,
[branchname] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[fdnum] [varchar] (53) COLLATE Chinese_PRC_CI_AS NULL ,
[fdname] [varchar] (30) COLLATE Chinese_PRC_CI_AS NULL ,
[cgid] [int] NULL ,
) ON [PRIMARY]
GO
数据库结构
http://topic.csdn.net/u/20091130/21/fb718680-98ff-4afb-98d8-cff2f8293ed5.html?24281
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
CREATE TABLE [dbo].[branch] (
[ltdid] [int] NOT NULL ,
[branchname] [varchar] (30) COLLATE Chinese_PRC_CI_AS NOT NULL ,
) ON [PRIMARY]
GO
insert into branch(ltdid,branchname)values('1032','aa分店')
insert into branch(ltdid,branchname)values('1032','bb分店')CREATE TABLE [dbo].[consignee] (
[cid] [int] IDENTITY (1, 1) NOT NULL ,
[cgid] [int] NOT NULL ,
[cgname] [varchar] (20) COLLATE Chinese_PRC_CI_AS NOT NULL ,
) ON [PRIMARY]
GO
insert into consignee(cgid,cgname)values('1001','点心')
insert into consignee(cgid,cgname)values('1002','蔬菜')CREATE TABLE [dbo].[orderlist] (
[listid] [int] IDENTITY (1, 1) NOT NULL ,
[ltdid] [int] NULL ,
[branchname] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
-- [fdnum] [varchar] (53) COLLATE Chinese_PRC_CI_AS NULL ,
--[fdname] [varchar] (30) COLLATE Chinese_PRC_CI_AS NULL ,
[cgid] [int] NULL ,
[checkout] [int] Null
) ON [PRIMARY]
GOinsert into orderlist(ltdid,branchname,cgid,checkout)values('1001','aa分店','1001',1)
insert into orderlist(ltdid,branchname,cgid,checkout)values('1001','aa分店','1002',0)
insert into orderlist(ltdid,branchname,cgid,checkout)values('1002','bb分店','1001',1)
insert into orderlist(ltdid,branchname,cgid,checkout)values('1002','bb分店','1002',1)
select * from consignee
select * from orderlist
1> select * from branch
2> select * from consignee
3> select * from orderlist
4> go
ltdid |branchname
-----------|------------------------------
1032|aa分店
1032|bb分店(2 rows affected)
cid |cgid |cgname
-----------|-----------|--------------------
1| 1001|点心
2| 1002|蔬菜(2 rows affected)
listid |ltdid |branchname |cgid |checkout
-----------|-----------|--------------------|-----------|-----------
1| 1001|aa分店 | 1001| 1
2| 1001|aa分店 | 1002| 0
3| 1002|bb分店 | 1001| 1
4| 1002|bb分店 | 1002| 1(4 rows affected)
1>建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
insert into branch(ltdid,branchname)values('1032','aa分店')
insert into branch(ltdid,branchname)values('1032','bb分店')
CREATE TABLE [dbo].[consignee] ([cid] [int] IDENTITY (1, 1) NOT NULL ,[cgid] [int] NOT NULL ,[cgname] [varchar] (20) NOT NULL)
insert into consignee(cgid,cgname)values('1001','点心')
insert into consignee(cgid,cgname)values('1002','蔬菜')
CREATE TABLE [dbo].[orderlist] ([listid] [int] IDENTITY (1, 1) NOT NULL ,[ltdid] [int] NULL ,[branchname] [varchar] (20) NULL ,[cgid] [int] NULL ,[checkout] [int] Null)
insert into orderlist(ltdid,branchname,cgid,checkout)values('1001','aa分店','1001',1)
insert into orderlist(ltdid,branchname,cgid,checkout)values('1001','aa分店','1002',0)
insert into orderlist(ltdid,branchname,cgid,checkout)values('1002','bb分店','1001',1)
insert into orderlist(ltdid,branchname,cgid,checkout)values('1002','bb分店','1002',1)select t.* , isnull(cast(p.checkout as varchar),'没有下单') 状态 from
(
select m.cgid , m.cgname 分店, n.ltdid , n.branchname 部门 from consignee m cross join branch n
) t
left join
orderlist p
on t.cgid= p.cgid and t.部门= p.branchnamedrop table branch,consignee,orderlist/*
cgid 分店 ltdid 部门 状态
----------- -------------------- ----------- ------------------------------ ------------------------------
1001 点心 1032 aa分店 1
1001 点心 1032 bb分店 1
1002 蔬菜 1032 aa分店 0
1002 蔬菜 1032 bb分店 1(所影响的行数为 4 行)
*/
楼主似乎根本不看别人给你的回复。 你这样,大家只能猜你想要什么。结果反而是浪费大家时间。建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
非常的抱歉,跟你说一声对不起。
我的orderlist表里还有一个字段,是ordertype订单的类型,比如“正单”,“加单”
consignee表里有个字段ordertype订单类型,比如“正单”,“加单”cgid 分店 ltdid 部门 类型 状态
----------- -------------------- ----------- ------------------------------ ------------------------------
1001 点心 1032 aa分店 正单 1
1001 点心 1032 bb分店 加单 1
1002 蔬菜 1032 aa分店 正单 0
1002 蔬菜 1032 bb分店 正单 1
最后我想要的结果就是这样
cgid,cgname,ordertype
1002,点心,正单
1002,点心,加单
1003,蔬菜,正单
1003,蔬菜,加单
我的orderlist表里还有一个字段,是ordertype订单的类型,比如“正单”,“加单”
consignee表里有个字段ordertype订单类型,比如“正单”,“加单” 到底是orderlist表?还是consignee表?
是否需要匹配?你自己去选吧.select t.* , isnull(cast(p.checkout as varchar),'没有下单') 状态, isnull(p.orderlist,'') orderlist from
(
select m.cgid , m.cgname 分店, n.ltdid , n.branchname 部门 from consignee m cross join branch n
) t
left join
orderlist p
on t.cgid= p.cgid and t.部门= p.branchname
select t.* , isnull(cast(p.checkout as varchar),'没有下单') 状态, t.orderlist from
(
select m.cgid , m.cgname 分店, m.orderlist n.ltdid , n.branchname 部门 from consignee m cross join branch n
) t
left join
orderlist p
on t.cgid= p.cgid and t.部门= p.branchname and t.orderlist = p.orderlist如果再不说清楚,不再回答此帖.
2> select * from consignee
3> select * from orderlist
4> go
ltdid |branchname
-----------|------------------------------
1032|aa分店
1032|bb分店(2 rows affected)
cid |cgid |cgname
-----------|-----------|--------------------
1| 1001|点心
2| 1002|蔬菜(2 rows affected)
listid |ltdid |branchname |cgid |checkout
-----------|-----------|--------------------|-----------|-----------
1| 1001|aa分店 | 1001| 1
2| 1001|aa分店 | 1002| 0
3| 1002|bb分店 | 1001| 1
4| 1002|bb分店 | 1002| 1
楼主啊你不要再玩了。你就一次性把所有东西贴完整! 1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
是的,我需要匹配
如果consignee 表的cgid有重复的字段,就会显示不对,没有加单的,状态也会显示为1