sql

 

用户信息表customerinfo

字段

字段含义

Gcustomerid

用户ID

customername

用户名

state

用户状态(4,正常,7删除)

 

计费名表cn

字段

字段含义

customerid

用户ID

Gcnid

计费名ID

cnname

计费名

state

状态(1,正常,2封禁)

 

每日搜索收入表daysearch

字段

字段含义

Gcnid

计费名ID

income

收入

date

日期

 

每日主题收入表daycpro

字段

字段含义

Gcnid

计费名ID

income

收入

date

日期

 

每日合作推广收入表daycpa

字段

字段含义

Gcnid

计费名ID

income

收入

date

日期

 

注:

1G代表主键
2)每个用户有多个计费名
3)如果某个计费名没有某项收入,则不计入相应的收入表中



1、查出所有以union为前缀的非删除用户,并且以其正常计费名个数多少降序排列。(计费名相同不重复计算)

     、今天是20100910,查出前30天之内,总收入小于100元的用户之中,收入最多的前100个用户。

 

 

 

   

3、查询同时具有网站搜索收入、主题推广收入、合作推广收入的正常状态用户名,以及每项收入的金额。

     

 

 

 

 

 

 

 

 

 

 




-------------------------------------------------------------------------------------------------------------------
练习2:请写出下面的sql语句

 

1、 查询出tb_order表中所有内容。

 

2、 查询出cust_id42206的客户的名字(tb_customer表)

 

3、 查询出帐户开户信息表(tb_account_info)里,开户金额(open_account_sum)大于5600的记录

 

4、 从订单信息表(tb_order)中查询出提单时间(submit_date)在20091210日提交的订单编号(code

 

5、 从订单信息表(tb_order)中查询出提单时间(submit_date)在2009-11-012009-12-09之间提交的订单,按时间逆序排序后,取最近的10

 

6、 计算出订单备注表(tb_order_comment)中订单idorder_id)为105的记录条数

 

7、 已知一个订单可能会有多条备注。需要查询出订单备注表(tb_order_comment)中,备注数目最多的前10个订单id

 

8、 更新id范围是100110的订单(tb_order)提单时间(submit_date)为当前时间

 

9、 查找出以“北京”开头的客户(tb_customer)资料

 

10、 已知订单表(tb_order)和订单备注表(tb_order_comment)以订单idtb_order_comment.order_id)关联,查询出订单编号(tb_order.code)为“XSBJ200910251452”的订单,所有的备注内容(tb_order_comment.content

 

11、已知订单表(tb_order)和订单备注表(tb_order_comment)以订单idtb_order_comment.order_id)关联,统计出订单备注条数最多的前10条订单,展现订单编号(tb_order.code)和备注条数

 

12、已知procedurelog表结构如下:

Field

Type

------

------------

id

int(10)

name

varchar(100)

date

datetime

status

smallint(3)


1
)删除procedurelog表里,操作时间(date)为2009-11-12日的记录

2)将如下值插入到表中   Id123nametestdate为当前时间,status1




----------------------------------------------------------------------------------------------------------
补充知识:UNION
UNION指令的使用
UNION 指令的目的是将两个 SQL 语句的结果合并起来。从这个角度来看, UNION 跟 JOIN 有些许类似,因为这两个指令都可以由多个表格中撷取资料。 UNION 的一个限制是两个 SQL 语句所产生的栏位需要是同样的资料种类。另外,当我们用 UNION这个指令时,我们只会看到不同的资料值 (类似 SELECT DISTINCT)。 union只是将两个结果联结起来一起显示,并不是联结两个表…………

假设我们有以下的两个表格,
Store_Information 表格 store_name Sales Date
Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
Los Angeles $300 Jan-08-1999
Boston $700 Jan-08-1999

Internet Sales 表格 Date Sales
Jan-07-1999 $250
Jan-10-1999 $535
Jan-11-1999 $320
Jan-12-1999 $750

而我们要找出来所有有营业额 (sales) 的日子。要达到这个目的,我们用以下的 SQL 语句: SELECT Date FROM Store_Information
UNION
SELECT Date FROM Internet_Sales 结果: Date
Jan-05-1999
Jan-07-1999
Jan-08-1999
Jan-10-1999
Jan-11-1999
Jan-12-1999
有一点值得注意的是,如果我们在任何一个 SQL 语句 (或是两句都一起) 用 "SELECT DISTINCT Date" 的话,那我们会得到完全一样的结果。

SQL Union All
UNION ALL 这个指令的目的也是要将两个 SQL 语句的结果合并在一起。 UNION ALL 和 UNION 不同之处在于 UNION ALL 会将每一笔符合条件的资料都列出来,无论资料值有无重复。 UNION ALL 的语法如下: [SQL 语句 1]
UNION ALL
[SQL 语句 2] 我们用和上一页同样的例子来显示出 UNION ALL 和 UNION 的不同。同样假设我们有以下两个表格, Store_Information 表格 store_name Sales Date
Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
Los Angeles $300 Jan-08-1999
Boston $700 Jan-08-1999

Internet Sales 表格 Date Sales
Jan-07-1999 $250
Jan-10-1999 $535
Jan-11-1999 $320
Jan-12-1999 $750

而我们要找出有店面营业额以及网络营业额的日子。要达到这个目的,我们用以下的 SQL 语句: SELECT Date FROM Store_Information
UNION ALL
SELECT Date FROM Internet_Sales 结果: Date
Jan-05-1999
Jan-07-1999
Jan-08-1999
Jan-08-1999
Jan-07-1999
Jan-10-1999
Jan-11-1999
Jan-12-1999

 

练习1:
我有三个用户表
普通用户、VIP用户、商业用户,这三个表之间没有关联。

比如我想查询名为张三是在哪张表中,这个联合语句怎么写? 高手帮帮忙

补充:这三个表字段都是普通的ID,USERNAME,PWD那些。没有关联是指没搞视图,索引的那些高级东西。

我现在想了个折中的办法,就是先把三个用户表名写进数组中,在遍历数组的同时判断表中是否含有名叫张三的用户,找到了就直接退出遍历。不知道这个方法对执行效率有没有影响?

网友参考答案:

假定3张表分别是normal_cus,vip_cus,ent_cus,
这3张表都包含字段:username,
可以这么写SQL:
select username,'normal_cus' as tab_name from normal_cus where username='张三'
union
select username,'vip_cus' as tab_name from vip_cus where username='张三'
union
select username,'ent_cus' as tab_name from ent_cus where username='张三'
 这样出来的结果中,就包含这个人所在的表名了。 当然,这里还有一个问题:姓名是唯一的吗?如果不唯一,那么可能多个表中都会出现这个人名。



练习2:
总共6个表Card,CardFillDate ,CardFill ,CardReplace,CardRecycle,Mainsavetable
每个表:
Card          表中有addtime,cardmoney字段
CardFillDate  表中有filldate,fillmoney字段
CardFill      表中有filldate,fillmoney字段
CardReplace   表中有replacedate,replacemoney字段
CardRecycle   表中有recycledate,returnmoney字段
Mainsavetable 表中有outdate,chargemoney字段
我想要的查询结果:
月份 发行收费 延期收费 充值收费 更换收费 回收退费 出场收费 小计
01   100      50       200      150      500      500      150

select a.yd,cardmoney,c.fillmoney,d.fillmoney as fillmoney1,replacemoney,returnmoney,chargemoney from (
select '1' as yd union select '2' union select '3' union
select '5' union select '6' union select '7' union
select '8' union select '9' union select '10' union
select '11' union select '12') a 
left join
(select month(addtime)as yd,sum(cardmoney)as cardmoney from Card where year(addtime) = '2009' group by month(addtime)) b
on a.yd=b.yd
left join 
(select month(filldate)as yd,sum(fillmoney)as fillmoney from CardFillDate where year(filldate) = '2009' group by month(filldate)) c
on a.yd=c.yd
left join 
(select month(filldate)as yd,sum(fillmoney)as fillmoney from CardFill where year(filldate) = '2009' group by month(filldate)) d
on a.yd=d.yd
left join 
(select month(replacedate)as yd,sum(replacemoney)as replacemoney from CardReplace where year(replacedate) = '2009' group by month(replacedate)) e
on a.yd=e.yd
left join 
(select month(recycledate)as yd,sum(returnmoney)as returnmoney from CardRecycle where year(recycledate) = '2009' group by month(recycledate)) f
on a.yd=f.yd
left join 
(select month(outdate)as yd,sum(chargemoney)as chargemoney from Mainsavetable where year(outdate) = '2009' group by month(outdate)) g
on a.yd=g.yd order by  cast(a.yd as int) 
 

或者:
   
select month(a.addtime) as '月份', sum(a.CardMoney) 发行收费,sum(b.FillMoney) 延期收费, sum(c.FillMoney) 充值收费, 
sum(d.ReplaceMoney) 更换收费, sum(e.ReturnMoney) 回收退费,sum(f.ChargeMoney) 出场收费 
from Card a,CardFillDate b,CardFill c,CardReplace d,CardRecycle e,Mainsavetable f
where year(a.addtime) = '2009' and month(a.addtime) in (1,2,3,4,5,6,7,8,9,10,)
and YEAR_MONTH(a.addtime)=YEAR_MONTH(b.filldate)
and YEAR_MONTH(a.addtime)=YEAR_MONTH(c.filldate)
and YEAR_MONTH(a.addtime)=YEAR_MONTH(d.replacedate)
and YEAR_MONTH(a.addtime)=YEAR_MONTH(e.recycledate)
and YEAR_MONTH(a.addtime)=YEAR_MONTH(f.outdate)
group by month(a.addtime) order by '月份' 
 

 

 

 

参考:

http://hi.baidu.com/%B5%BE%CB%EB%C9%CF%B5%C4%D1%CC/blog/item/e13fbc00a0ef17dc277fb5f7.html

 

http://www.ixpub.net/viewthread.php?tid=950791

 

http://www.ixpub.net/viewthread.php?tid=935801

 

 

access:
select top (10) * from table1

db2:
select 列名 from table1 where 1=1 fetch first 10 rows only

mysql:
select * from table1 limit 10 或 select * from table1 limit 0,10

sql server:
读取前10条:select top (10) * from table1 where 1=1

在sqlserver里面,若每页显示perpage条记录,按照id的排序,取第pageno页的记录:
select top perpage*pageno   * from table where id not in(select top (pageno-1)*perpage id from table1)

oracle取前10条:
select * from table1 where rownum<=10

oracle取第11-20条:

select * from (select *,rownum num from table1 where rownum<=20) where num>=10;

oracle按id排序后取11-20条

select * from (select *,rownum num from (select * from table1 tt order by id desc) where rownum<=20) where num>=10;

关于mysql 的limit 子句的使用方法:

SELECT * FROM table    LIMIT [ offset, ] rows | rows OFFSET offset


LIMIT 子句可以被用于强制 SELECT 语句返回指定的记录数。LIMIT 接受一个或两个数字参数。参数必须是一个整数常量。如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目。初 始记录行的偏移量是 0(而不是 1): 为了与 PostgreSQL 兼容,MySQL 也支持句法: LIMIT # OFFSET #。

mysql > SELECT * FROM table LIMIT 5 , 10 ;   // 检索记录行 6 - 15

// 为了检索从某一个偏移量到记录集的结束所有的记录行,可以指定第二个参数为 - 1
mysql
> SELECT * FROM table LIMIT 95 , - 1 ; // 检索记录行 96 - last.

// 如果只给定一个参数,它表示返回最大的记录行数目:
mysql
> SELECT * FROM table LIMIT 5 ;      // 检索前 5 个记录行

// 换句话说,LIMIT n 等价于 LIMIT 0 ,n。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值