手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表分类:DataBase

常用SQL整理

  说明:复制表(只复制结构,源表名:a 新表名:b)
      SQL: select * into b from a where 1<>1  (mysql无法执行

  说明:拷贝表(拷贝数据,源表名:a 目标表名:b)

  SQL: insert into b(a, b, c) select d,e,f from b;

  说明:显示文章、提交人和最后回复时间[这个还是用innerJOIN较好]

  SQL: select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b

  说明:外连接查询(表名1:a 表名2:b)[MYSQL已经支持using函数]

  SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c

  说明:日程安排提前五分钟提醒[MYSQL无getdate函数]

  SQL: select * from 日程安排 where datediff('minute',开始时间,getdate())>5

  说明:两张关联表,删除主表中已经在副表中没有的信息

  SQL:

  delete from info where not exists ( select * from infobz where info.infid=infobz.infid )

  说明:看看即可,不要深究

  SQL:

  SELECT A.NUM, A.NAME, B.UPD_DATE, B.PREV_UPD_DATE

  FROM TABLE1,

  (SELECT X.NUM, X.UPD_DATE, Y.UPD_DATE PREV_UPD_DATE

  FROM (SELECT NUM, UPD_DATE, INBOUND_QTY, STOCK_ONHAND

  FROM TABLE2

  WHERE TO_CHAR(UPD_DATE,'YYYY/MM') = TO_CHAR(SYSDATE, 'YYYY/MM')) X,

  (SELECT NUM, UPD_DATE, STOCK_ONHAND

  FROM TABLE2

  WHERE TO_CHAR(UPD_DATE,'YYYY/MM') =

  TO_CHAR(TO_DATE(TO_CHAR(SYSDATE, 'YYYY/MM') ¦¦ '/01','YYYY/MM/DD') - 1, 'YYYY/MM') ) Y,

  WHERE X.NUM = Y.NUM (+)

  AND X.INBOUND_QTY + NVL(Y.STOCK_ONHAND,0) <> X.STOCK_ONHAND ) B

  WHERE A.NUM = B.NUM

  说明:--

  SQL:

   select * from studentinfo where not exists(select * from student where studentinfo.id=student.id) and 系名称='"&strdepartmentname&"' and 专业名称='"&strprofessionname&"' order by 性别,生源地,高考总成绩

  说明:   从数据库中去一年的各单位电话费统计(电话费定额贺电化肥清单两个表来源)

  SQL:

  SELECT a.userper, a.tel, a.standfee, TO_CHAR(a.telfeedate, 'yyyy') AS telyear,

  SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '01', a.factration)) AS JAN,

  SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '02', a.factration)) AS FRI,

  SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '03', a.factration)) AS MAR,

  SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '04', a.factration)) AS APR,

  SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '05', a.factration)) AS MAY,

  SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '06', a.factration)) AS JUE,

  SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '07', a.factration)) AS JUL,

  SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '08', a.factration)) AS AGU,

  SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '09', a.factration)) AS SEP,

  SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '10', a.factration)) AS OCT,

  SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '11', a.factration)) AS NOV,

  SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '12', a.factration)) AS DEC

  FROM (SELECT a.userper, a.tel, a.standfee, b.telfeedate, b.factration

  FROM TELFEESTAND a, TELFEE b

  WHERE a.tel = b.telfax) a

  GROUP BY a.userper, a.tel, a.standfee, TO_CHAR(a.telfeedate, 'yyyy')

  说明:四表联查问题:

  SQL: select * from a left inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where .....

  说明:得到表中最小的未使用的ID号

  SQL:

  SELECT (CASE WHEN EXISTS(SELECT * FROM Handle b WHERE b.HandleID = 1) THEN MIN(HandleID) + 1 ELSE 1 END) as HandleID

  FROM Handle

  WHERE NOT HandleID IN (SELECT a.HandleID - 1 FROM Handle a)

原文来自:精妙SQL语句精华

Tags: sqlserver

一句SQL:取得相同sortid的结果集,每种 ID三条数据

一个表:

SQL代码
  1. CREATE TABLE `book` (   
  2.   `bookid` int(11) unsigned NOT NULL AUTO_INCREMENT,  
  3.   `sortid` int(11) NOT NULL,  
  4.   `bookname` varchar(255) NOT NULL DEFAULT '',  
  5.   PRIMARY KEY (`bookid`)  
  6. ) ENGINE=InnoDB AUTO_INCREMENT=80729 DEFAULT CHARSET=utf8;  

数据是这样的:

XML/HTML代码
  1. insert  into `book`(`bookid`,`sortid`,`bookname`) values (1,1,'1'),(2,1,'2'),(3,1,'3'),(4,1,'4'),(5,2,'1'),(6,2,'2'),(7,2,'3'),(8,2,'4'),(9,2,'5'),(10,2,'6'),(11,2,'7'),(12,2,'8'),(13,2,'9'),(14,2,'10'),(15,2,'11'),(16,3,'1'),(17,3,'2'),(18,3,'3'),(19,3,'4'),(20,3,'5'),(21,3,'6'),(22,3,'7'),(23,3,'8'),(24,3,'9'),(25,3,'10'),(26,3,'11'),(27,3,'12'),(28,4,'1'),(29,4,'2'),(30,4,'3'),(31,4,'4'),(32,4,'5'),(33,5,'1'),(34,5,'2'),(35,5,'3'),(36,5,'4');  

在这种情况下,要取得 sortid IN (1,2,3)的数据,每个sortid的结果是3条。
这种题目网上很多,但真的没有一种特别好SQL,在网上有很多种。最初的时候,我自己是想着用union来处理,但最终这种方法太伤了,同事clear提出的SQL不错,试了一下,在几万条数据的时候,效率还可以,而且SQL相对简单:

SQL代码
  1. SELECT sortid,bookname FROM book b WHERE sortid IN(1,2) AND 3>(SELECT COUNT(1) FROM book WHERE b.sortid=sortid AND bookid<b.bookid)   

表自关联,在sortid上建索引,效率还不错。

一句可以围观的SQL

窝窝(Walkerlee)最近说他折腾了一个SQL,提升了他的工作效率。表示要到博客来投稿,今天我把这个SQL放出来了。
嗯。我总不能说,因为我确实会这个SQL就断了他投稿的心,否则他会砍我的。

前因:
数据库结构如下:

要求:一句SQL实现,分别统计type1,2,3,4 一共多少money (group by uid)

原先做法:分开查询 然后用php合并结果,如

SQL代码
  1. select sum(money) form test where type = 1 group by uid  
  2. select sum(money) form test where type = 2 group by uid  
  3. select sum(money) form test where type = 3 group by uid  
  4. select sum(money) form test where type = 4 group by uid  

现在要求一句SQL。
窝窝问我的时候,我说if(a,b,c)这个函数可以解决,于是他很愤怒。。。。
贴出了最终的SQL语句:

SQL代码
  1. SELECT  
  2. uid,  
  3. SUM( IF(`type`=1, money, 0 ) ) AS `BuyByCash`,  
  4. SUM( IF (`type`=3, money, 0 ) ) AS `BuyByCard`,  
  5. SUM( IF( `type` IN (1,3), money, 0 ) ) AS `BuyByCashAndCard`,  
  6. SUM( IF(`type`=2, money, 0 ) ) AS `SellByCash`,  
  7. SUM( IF(`type`=4, money, 0 ) ) AS `SellByCard`,  
  8. SUM( IF( `type` IN (2,4), money, 0 ) ) AS `BuyByCashAndCard`,  
  9. SUM(money) AS `Amount`,  
  10. COUNT(*) AS `DataNums`  
  11. FROM `test`   
  12. group by `uid`  

窝窝比较郁闷,因为以前处理起来很麻烦,而现在一句就解决了。


此事完结后,他又介绍了一个小技巧:
我有一个relation表,用来存储订单下面的游客

SQL代码
  1. ALTER TABLE `travel_order_customer_relation`  
  2.   ADD CONSTRAINT `travel_order_customer_relation_ibfk_1` FOREIGN KEY (`oid`) REFERENCES `travel_orders` (`id`) ON DELETE CASCADE

然后我用了一个外键,这样order被删除时候,这个关系表里面的相关数据也就自动删除了
----
然后我也很郁闷的说,我的数据表不是INNODB的,所以这个功能我不能用。。。也很纠结

Tags: mysql, group, sum, if

Infomation:WebKit Does HTML5 Client-side Database Storage

资料备份,其中有两个链接很重要 :
原资料中的:client-side database storage.已经打不开了,好象是换成这个了:10.1.2.2 A worker for updating a client-side database
但我也说不准,详情还是看这个页面吧:http://www.whatwg.org/specs/web-apps/current-work/multipage/

对了,因为是基于webkit的,所以,FF上是没法用的。更多的还是用于ipad,iphone之类的上面吧。

jQueryMobile的代码里应该是有了

WebKit Does HTML5 Client-side Database Storage

Posted by Brady Eidson on Friday, October 19th, 2007 at 4:04 pm

The current working spec for the HTML5 standard has a lot of exciting features we would eventually like to implement in WebKit. One feature we felt was exciting enough to tackle now even though the spec is still in flux is client-side database storage. So for the last few weeks andersca, xenon, and I have been cooking up an implementation!

The client-side database storage API allows web applications to store structured data locally using a medium many web developers are already familiar with – SQL.

The API is asynchronous and uses callback functions to track the results of a database query.
Compact usage defining a callback function on the fly might look something like this:

var database = openDatabase("Database Name", "Database Version");

database.executeSql("SELECT * FROM test", function(result1) {
   // do something with the results
   database.executeSql("DROP TABLE test", function(result2) {
     // do some more stuff
     alert("My second database query finished executing!");
   });
});

There will also be a small example of how to use the API in a real site that we’ll try to keep up to date as things evolve.

This initial implementation has some things missing from the spec as well as a few known bugs. But it does the basics and the best way to discover what needs work is to get it out there for people to start using it!

If you find any bugs, would like to suggest features, or have gripes about the spec itself, please drop by #webkit or drop us a line on the WebKit email lists.

Oh, and one more thing…

We’re landing this initial implementation with pretty cool Web Inspector support!
So far you can view the full contents of any table and run arbitrary queries on each database a page is using. We have a lot of ideas for improvements but would also love to hear yours.
DatabaseInspector

Tags: html5, database, storage

update sql

更新数据库的时候,提示: You can't specify target table 'channels' for update in FROM clause
出错的SQL是什么呢?
出错的是SQL是:
update channels set parentid = 0 where parentid in (select b.id from channels b where b.parentid = 0) ;
从理论上来看好象没什么错,但就是出现上面的错误(You can't specify target table 'channels' for update in FROM clause),说是不能在原表操作。于是改正它。。
改成:
update channels set parentid = 0 where parentid IN ( select id from (SELECT id from channels where parentid = 0) as tmp )
看上去是不是很恶心?但确实是通过临时表来解决了这个问题。。。
好吧,我又恶心了

Tags: update, 子查询