求一条SQL 语句的写法?很简单,会就不难(200分)

  • 主题发起人 主题发起人 linger_ok
  • 开始时间 开始时间
L

linger_ok

Unregistered / Unconfirmed
GUEST, unregistred user!
表A editor_id lb2_id
123 000
123 003
123 003
456 007
456 006
表B lb2_id lb2_name
000 a
003 b
006 c
007 d
显示 a 共1条 (表A内lb2_id为000的条数)
b 共2条(表A内lb2_id为003的条数)
很简单吧,呵呵,拜托各位老大了
 
a.
select A.editor_id, A.lb2_id, B.lb2_name
from 表A A join 表B B on B.lb2_id=A.lb2_id
where A.lb2_id='000'
b.
select A.editor_id, A.lb2_id, B.lb2_name
from 表A A join 表B B on B.lb2_id=A.lb2_id
where A.lb2_id='003'


 
不是这样,000和003都是未知的,只能从数据库中读取
 
select A.editor_id, A.lb2_id, B.lb2_name
from 表A A ,表B B on B where A.lb2_id=b.lb2_id
 
select T2.lb2_name,T1.条数 from
(select lb2_id,count(lb2_id) 条数 from 表a group by lb2_id) T1,
表b t2 where t1.lb2_id=t2.lb2_id
 
select c.lb2_name,a.cc from 表B c,(select lb2_id,count(lb2_id) as cc from 表A group by lb2_id) a
where c.lb2_id=a.lb2_id
靠,迟了几分钟
 
更直观点:
select c.lb2_name,'共',a.cc,'条' from 表B c,(select lb2_id,count(lb2_id) as cc from 表A group by lb2_id) a
where c.lb2_id=a.lb2_id
 
select b.lb2_name, Count(b.lb2_name)
from 表A A join 表B B on B.lb2_id=A.lb2_id
group by b.lb2_name
 
呵呵,楼上的大哥,你可真是热心,我一定把分给你!
但是我是是看不明白,
a.cc是什么意思啊?
表B c又是什么意思啊?
 
select c.lb2_name,'共',a.cc,'条' from 表B c,
(select lb2_id,count(lb2_id) as cc from 表A group by lb2_id) a
where c.lb2_id=a.lb2_id


表B c,把表B用别名c代替
(select lb2_id,count(lb2_id) as cc from 表A group by lb2_id) a
把查询语句select lb2_id,count(lb2_id) as cc from 表A group by lb2_id得到的
结果当成一个表来使用,表明为a,
count(lb2_id) as cc 把字段count(lb2_id)用别名cc代替
a.cc表a中的cc地段
够清楚了吧!!!!!!!!!!1

 
select b.lb2_name,count(a.lb2_id) from tableA a,tableB b
where a.lb2_id=b.lb2_id
group by b.lb2_name
 
kao,写完刷新一看这么多贴啦!!不过我还是要发!
我来回答看不懂部分!
(呵呵,骗点分)
这是SQL Server 的写发:
select c.lb2_name,'共',a.cc,'条' from 表B c,(select lb2_id,count(lb2_id) as cc from 表A group by lb2_id) a
where c.lb2_id=a.lb2_id
Oracle 的写发:
select c.lb2_name,'共',a.cc,'条'
from 表B as c,
(select lb2_id,count(lb2_id) as cc from 表A group by lb2_id) as a
where c.lb2_id=a.lb2_id

说明:
a 为你上边说的表A进行查询后的试图:
(select lb2_id,count(lb2_id) as cc from 表A group by lb2_id) as a
a.cc 为上边试图的:count(lb2_id) as cc ; 是a的列(在有的书里面说是Field)


c 为你上边说的表B: 表B as c

(不过你是应该找本书看看哦!!!:)或者看看onlinehelp )
 
book523已经把我的代码解释得很清楚了,我也没必要再说了。
多谢book523。
 
樓上的就基本可以了吧,就不用再寫了
 
200芬浪费
 
这样只是显示一条结果啊!
我想统计的是不同的lb2_name的各个条数
 
select count(lb2_id) from tableA where lb2_id in(select lb2_id from tableb where lb2_name='****')
 
lb2_name='****'
是不行的啊,***是库里面的东西啊
 
你需要什么样的语名呀,
BEGIN TRANSACTION
SELECT @PDBH=0
SELECT @PDBH=MAX(CHECKCODE) FROM STO_CHECK
SELECT @PDBH=ISNULL(@PDBH,0)+1
UPDATE STO_LSCHECK SET CHECKCODE=@PDBH
IF @@error!=0
BEGIN
ROLLBACK TRANSACTION
RETURN -10000
END
INSERT INTO STO_CHECK SELECT * FROM STO_LSCHECK
IF @@error !=0
BEGIN
ROLLBACK TRANSACTION
RETURN -10000
END
DECLARE CHECK_CUR CURSOR FOR
SELECT METCODE,ACCOUNTSUM,FACTSUM,CHECKER,CHECKTIME FROM STO_LSCHECK WHERE ACCOUNTSUM<>FACTSUM
OPEN CHECK_CUR
FETCH NEXT FROM CHECK_CUR INTO @WZBH,@ZMSL,@SPKC,@PDR,@PDSJ
WHILE @@FETCH_STATUS =0
BEGIN
SELECT @YKSL=@SPKC-@ZMSL
SELECT @LSH=0
SELECT @LSH=MAX(ORDERCODE) FROM STO_INANDOUT
SELECT @LSH=ISNULL(@LSH,0)+1
IF @YKSL>0
BEGIN --盘盈,库存量增加,平均成本下降
SELECT @KCCB=STOCOST FROM STO_METINFO WHERE METCODE=@WZBH

INSERT INTO STO_INANDOUT ( ORDERCODE,METCODE,SUPPLY,AMOUNT,INOUTSIGN,INOUTSHAPE,TRANSACTOR,TRANSTIME,STONUMBER,STOCOST)
VALUES (@LSH,@WZBH,'盘点',@YKSL,'I','R',@PDR,@PDSJ,@SPKC,@KCCB)
IF @@ERROR !=0
BEGIN
CLOSE CHECK_CUR
DEALLOCATE CHECK_CUR
ROLLBACK TRANSACTION
RETURN -10000
END
END
ELSE
BEGIN --盘亏,库存量减少,平均成本上升
SELECT @KCCB=STOCOST FROM STO_METINFO WHERE METCODE=@WZBH
SELECT @YKSL=ABS(@YKSL)
INSERT INTO STO_INANDOUT ( ORDERCODE,METCODE,SUPPLY,SHIPMENT,INOUTSIGN,INOUTSHAPE,TRANSACTOR,TRANSTIME,STONUMBER,STOCOST)
VALUES (@LSH,@WZBH,'盘点',@YKSL,'O','D',@PDR,@PDSJ,@SPKC,@KCCB)
IF @@ERROR !=0
BEGIN
CLOSE CHECK_CUR
DEALLOCATE CHECK_CUR
ROLLBACK TRANSACTION
RETURN -10000
END
END
UPDATE STO_METINFO SET STONUMBER=@SPKC WHERE METCODE=@WZBH
IF @@ERROR !=0
BEGIN
CLOSE CHECK_CUR
DEALLOCATE CHECK_CUR
ROLLBACK TRANSACTION
RETURN -10000
END
FETCH NEXT FROM CHECK_CUR INTO @WZBH,@ZMSL,@SPKC,@PDR,@PDSJ
END
DELETE FROM STO_LSCHECK
IF @@ERROR !=0
BEGIN
CLOSE CHECK_CUR
DEALLOCATE CHECK_CUR
ROLLBACK TRANSACTION
RETURN -10000
END
CLOSE CHECK_CUR
DEALLOCATE CHECK_CUR
COMMIT TRANSACTION
RETURN 0
GO
这些够了吧
 
什么呀?
老大,我只要一条SQL语句啊
 
后退
顶部