查询图书表中各书已入库多少年?用sql select语句句实现

sql中如何用select 语句实现跨表计数查询?_百度知道
sql中如何用select 语句实现跨表计数查询?
表a为姓名 单位代码 身份证号 性别张三
X表b为单位代码 单位名称001
XXXXXX如何通过sql语句实现下列跨表计数查询单位名称
单位人数 女性人数 男性人数
提问者采纳
男&#39select
单位名称;女&#39,sum(case when 性别=&#39,count(*),sum(case when 性别=' then 1 else 0 end ).单位代码=b; then 1 else 0 end) from a
提问者评价
非常感谢~~~~~
其他类似问题
按默认排序
其他4条回答
(select 单位代码, (select 单位代码.nvnum as 女生人数, (select 单位代码,tb.单位代码 and
td.单位代码 and td.mannum as 男人数from
单位名称.单位名称select td.单位代码=) as
tb,单位代码 from 表b) as td
,count(姓名) as nanNum from 表a where 性别=&#39,count(姓名) as nvNum from 表a where 性别=&#39,ta.Num as 单位人数.单位代码=女&#39,tc,count(姓名) as Num from 表a
td.单位代码=男&#39
name:a表中姓名,sex:a表中性别,dno:单位代码select b.dname,
count(1) total,
sum(decode(a.sex,1,1,0)) male,
sum(decode(a.sex,2,1,0)) femalefrom a,bwhere b.dno=a.dnogroup by b.十四级的那位答案是正确的,最佳答案有一些小错误。
正常的要用join。join分inner join,outter join,left join,right join。这种情况用inner join,但根据不同的数据库软件,inner join有时候直接用join代替,所以你自己试一下该不该有inner。select 单位名称,count(*),sum(case when 性别='女' then 1 else 0 end ),sum(case when 性别='男' then 1 else 0 end) from a join b on a.单位代码=b.单位代码分步考虑,先不管b表:select
`单位代码`,
count(*) as '单位人数',
sum(case when 性别='女' then 1 else 0 end) as '女性人数',
sum(case when 性别='男' then 1 else 0 end) as '男性人数'from `a` group by `单位代码`;然后加b表进去:select `单位名称`, `单位人数`, `女性人数`, `男性人数` from(select
`单位代码`,
count(a.*) as '单位人数',
sum(case when 性别='女' then 1 else 0 end) as '女性人数',
sum(case when 性别='男' then 1 else 0 end) as '男性人数'from `a` group by `单位代码`) as cjoin b on c.`单位代码` = b.`单位代码`;
select b.单位名称, count(1) 单位人数, & & & sum(case&when a.性别 = '女' then&1&when a.性别 = '男' then&0&end) as 女性人数, & & & sum(case&when a.性别 = '男' then&1&when a.性别 = '女' then&0&end) as 男性人数 &from a, b where a.单位代码 = b.单位代码 group by b.单位名称
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁在ORACL数据库中表table1有字段codeid和cedename两个字段,表table2中也有codeid和codename字段,如何用一条SQL语句将这两张表的两个字段查询出来
采纳的答案
select codeid,cedename from table1
union all
select codeid,cedename from table2
~~~~
会把2张表的数据汇总在一起
没看懂,lz到底想干什么
select t1.codeid ,t1.cedename t2.cedename,t2.codeid ,tafrom table1 t1, table2 t2
没什么特殊要求吗?要没有的话上面的回答都能满足
说说你的要实现的目的,应该不是简单的把两个表的内容全部查询吧!两者表字段完全一样的字段之间有什么联系。
查询 这两张表的数据
&& select t1.codeid ,t1.cedename from& table1 t1
&&&& union all
&& select t2.codeid ,t2.cedename from& table1 t2
查询 这两张表的数据(去掉重复数据)
&& select t1.codeid ,t1.cedename from& table1 t1
&&&& union
&& select t2.codeid ,t2.cedename from& table1 t2
select * from table1 inner join table2 on table1.codeid=table2.codeid
已解决问题
未解决问题怎样用SQL语句查询一个数据库中的所有表?
怎样用SQL语句查询一个数据库中的所有表?
怎样用SQL语句查询一个数据库中的所有表?
&请各位帮忙解答一下,谢谢!
SQL& :& select * from information_schema.tables
ORACLE: select table_name from user_tables&
ACCESS: select&&& name&&& from&&& MSysObjects&&& where&&& type=1&&& and&&& flags=0
希望采纳谢谢~!
如果是mysql
use databasename;如果是sqlserver
就用 select * from sysobjects where xtype=&U&如果是oracle 就用 select * from tabs
希望能帮到你。
其他回答 (4)
SELECT * FROM
sysobjects s WHERE s.xtype=&U&
SELECT * FROM
sysobjectsWHERE xtype=&U&
--读取库中的所有表名 select name from sysobjects where xtype='u'
--读取指定表的所有列名 select name from syscolumns where id=(select max(id) from sysobjects where xtype='u' and name='表名') 获取数据库表名和字段 sqlserver中各个系统表的作用 &&&& sysaltfiles&&&&&& 主数据库&&&&&&&&&&&&&&&&& 保存数据库的文件syscharsets&&&&&& 主数据库&&&&&&&&&&&&&&&&& 字符集与排序顺序sysconfigures&&&& 主数据库&&&&&&&&&&&&&&&&& 配置选项syscurconfigs&&&& 主数据库&&&&&&&&&&&&&&&&& 当前配置选项sysdatabases&&&&& 主数据库&&&&&&&&&&&&&&&&& 服务器中的数据库syslanguages&&&&& 主数据库&&&&&&&&&&&&&&&&& 语言syslogins&&&&&&&& 主数据库&&&&&&&&&&&&&&&&& 登陆帐号信息sysoledbusers&&&& 主数据库&&&&&&&&&&&&&&&&& 链接服务器登陆信息sysprocesses&&&&& 主数据库&&&&&&&&&&&&&&&&& 进程sysremotelogins主数据库&&&&&&&&&&&&&&&&& 远程登录帐号
syscolumns&&&&&&& 每个数据库&&&&&&&&&&&&&&& 列sysconstrains&&&& 每个数据库&&&&&&&&&&&&&&& 限制sysfilegroups&&&& 每个数据库&&&&&&&&&&&&&&& 文件组sysfiles&&&&&&&&& 每个数据库&&&&&&&&&&&&&&& 文件sysforeignkeys 每个数据库&&&&&&&&&&&&&&& 外部关键字sysindexs&&&&&&&& 每个数据库&&&&&&&&&&&&&&& 索引sysmenbers&&&&&&& 每个数据库&&&&&&&&&&&&&&& 角色成员sysobjects&&&&&&& 每个数据库&&&&&&&&&&&&&&& 所有数据库对象syspermissions 每个数据库&&&&&&&&&&&&&&& 权限systypes&&&&&&&&& 每个数据库&&&&&&&&&&&&&&& 用户定义数据类型sysusers&&&&&&&&& 每个数据库&&&&&&&&&&&&&&& 用户
//用什么方法可以得到一个表中所有的列名。SQl语句。select&&&&& 列名=name&&&&& from&&&&& syscolumns&&&&& where&&&&& id=object_id(N'要查的表名')
select * from aunion allselect * from bunion allselect * from c
相关知识等待您来回答
编程领域专家其他回答(2)
select 型号表.型号,领料表.领用个数,入库表.入库数量 from 型号表 left join 领料表 on&型号表.型号=领料表.型号 left join 入库表 on&型号表.型号=入库表.入库数量
(跟两个表联查是一样的)
create table tbla
typename varchar(50)
insert into tbla
select 'dt-051' union all
select 'dt-052' union all
select 'dt-053' union all
select 'dt-054' union all
select 'dt-055' union all
select 'dt-056'
create table tblb
typename varchar(50),
insert into tblb
select 'dt-056',30 union all
select 'dt-056',20 union all
select 'dt-051',10 union all
select 'dt-052',30 union all
select 'dt-053',20 union all
select 'dt-051',30
create table tblc
typename varchar(50),
insert into tblc
select 'dt-056',30 union all
select 'dt-055',20 union all
select 'dt-056',20 union all
select 'dt-055',30 union all
select 'dt-051',50 union all
select 'dt-053',10
select a.typename as 型号,sum(isnull(b.num,0)) as 共领料,sum(isnull(c.num,0)) as 共入库
from tbla as a
left join tblb as b on b.typename = a.typename
left join tblc as c on c.typename = a.typename group by a.typename
order by a.typename asc
select a.typename,isnull(b.sumnum,0) as 工领料,isnull(c.sumnum,0) as 共入库
from tbla as a
left join (select typename,sum(num) as sumnum from tblb group by typename)
b on b.typename = a.typename
left join (select typename,sum(num) as sumnum from tblc
group by typename)
c on c.typename = a.typename
order by a.typename asc/*dt-051&& &40&& &50dt-052&& &30&& &0dt-053&& &20&& &10dt-054&& &0&& &0dt-055&& &0&& &50dt-056&& &50&& &50*/
&&&您需要以后才能回答,未注册用户请先。

我要回帖

更多关于 select语句大全 的文章

 

随机推荐