Posts

Showing posts with the label SQL

[Level 3] Rownum/auto_increment, substring, reverse index, case and mutli-table update for MySQL

-- [rownum] -- set initial equipment_id select max(equipment_id) from device into @rownum;   -- insert into equipment insert into device select @rownum:=ifnull(@rownum,-1)+1 from device; -- [multi-table update] update event inner join device using (deviceid) inner join equipment on device.macaddress = equipment.mac_address set event.equipment_id = equipment.equipment_id where event.eventtypeid < 400 ; -- [reverse index] select instr(c,','), substring(c,1,instr(c,',')-1), reverse(substring(c,1,instr(c,',')-1)), reverse(substring(reverse(substring(c,1,instr(c,',')-1)),1,instr(reverse(substring(c,1,instr(c,',')-1)),'@')-1)) from ( select 'a@1:2:3:4,x,y,z' as c union all select '1:2:3:4,x,y,z' as c union all select 'a@b@1:2:3:4,x,y,z' as c ) as t -- [case] update types set id =   ( select case types.id when 1 then 10001 when 2 then 10002 when 3 then 10003 when 4 then 10004 else types.id...

[Info] Top 10 SQL Performance Tips

Top 10 SQL Performance Tips, please refer to the link: http://forge.mysql.com/wiki/Top10SQLPerformanceTips Wish this helps. regards, Stanley Huang

[Level 2] How to get the difference between two tables.

Two table have the same data in echo column, how to get the difference between two tables. You can use out join and constraint the condition. Example: mysql> create table table1 (id int, name varchar(32)); mysql> create table table2 (id int, name varchar(32)); mysql> insert into table1 values(1,'Stanley'),(2,'John'),(4,'Mary'); mysql> insert into table2 values(1,'Stanley'),(3,'Joseph'),(5,'Christy'); case 1: find the data in table1 but not in table2; mysql> select * from table1 left join table2 on table1.id=table2.id where table2.id is null; case 2: find the data in table2 but not in table1; mysql> select * from table2 left join table1 on table2.id=table1.id where table1.id is null; Wish this helps. regards, Stanley Huang

[Books] SQL Performance Tuning

Image
A good book for study SQL performance tuning. The introduction in Amazon: http://www.amazon.com/SQL-Performance-Tuning-Peter-Gulutzan/dp/0201791692/ref=sr_1_1?ie=UTF8&qid=1264478735&sr=8-1 Wish this helps. regards, Stanley Huang