SQL Server 2008 R2——PIVOT 行转列 以及聚合函数的选择
==================================声明==================================
本文原创,转载在正文中显要的注明作者和出处,并保证文章的完整性。
未经作者同意请勿修改(包括本声明),保留法律追究的权利。
未经作者同意请勿用于学术性引用。
未经作者同意请勿用于商业出版、商业印刷、商业引用。
本文不定期修正完善,为保证内容正确,建议移步原文处阅读。
本文链接:http://www.cnblogs.com/wlsandwho/p/4423956.html
=======================================================================
看了好长时间的资料,总算是看懂了点。(另外怎样才能把右面的这个长毛的土豆去掉?----> 对,就是它!)
赶紧记录下来。
=======================================================================
表格和数据。(和http://www.cnblogs.com/wlsandwho/p/4416222.html的相同)
USE tempdb
GO
--------------------------------------------------------------------------
IF OBJECT_ID('Student','U') IS NOT NULL
DROP TABLE Student
GO CREATE TABLE Student
(
StuID NVARCHAR(8) PRIMARY KEY,
Name NVARCHAR(5),
EntranceTime DATETIME
)
GO INSERT INTO Student(StuID,Name,EntranceTime) VALUES('','Lily','2008-08-27')
INSERT INTO Student(StuID,Name,EntranceTime) VALUES('','Lucy','2009-08-26')
INSERT INTO Student(StuID,Name,EntranceTime) VALUES('','Jack','2007-08-28')
GO
--------------------------------------------------------------------------
IF OBJECT_ID('SltCourse','U') IS NOT NULL
DROP TABLE SltCourse
GO CREATE TABLE SltCourse --SelectiveCourse
(
ID INT PRIMARY KEY IDENTITY(1,1),
StuID NVARCHAR(8),
CourseName NVARCHAR(10),
Score INT
)
GO INSERT INTO SltCourse(StuID,CourseName,Score) VALUES('','电脑维修',90)
INSERT INTO SltCourse(StuID,CourseName,Score) VALUES('','剪纸',80)
INSERT INTO SltCourse(StuID,CourseName,Score) VALUES('','市场策划',95)
INSERT INTO SltCourse(StuID,CourseName,Score) VALUES('','信息检索',100) INSERT INTO SltCourse(StuID,CourseName,Score) VALUES('','插花',99)
INSERT INTO SltCourse(StuID,CourseName,Score) VALUES('','剪纸',96)
INSERT INTO SltCourse(StuID,CourseName,Score) VALUES('','刺绣',92) INSERT INTO SltCourse(StuID,CourseName,Score) VALUES('','插花',98)
GO
查看一下
SELECT s.StuID,s.Name,s.EntranceTime,sc.CourseName,sc.Score FROM Student s,SltCourse sc WHERE s.stuid=sc.stuid
GO
=======================================================================
现在来看这个表格,前三列有很多数据是重复的,而且也不便于阅读。之前使用了row_number函数,这里使用下别的——PIVOT来实现一个行转列。
预期效果。
=======================================================================
静态版。这需要预知要转换成列名的数据。
WITH ReportCard
AS(
SELECT s.StuID,s.Name,s.EntranceTime,sc.CourseName,sc.Score FROM Student s,SltCourse sc WHERE s.stuid=sc.stuid
)
SELECT * FROM ReportCard PIVOT(MAX(Score) FOR CourseName IN([电脑维修],[剪纸],[市场策划],[信息检索],[插花],[刺绣]) ) AS T
GO
结果。
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAlYAAABnCAIAAABNfptpAAAaZElEQVR4nO1dTW/cOLblr0lHXcta5TcEjUbK0Lp3tW9ggEY/O0BtA2QbA4UGgjRip4EazCZvmY170ZtYSfoNqieDGfjNeNLTk+dOENvxZ9mB3kJfFHn5JYoSWXUPCFvFosjDe6V7RElFkjRA3Lv/4BCBQCAQCDuQvuWsCe7df+B/6tuzCAQCgVAgVAm89hsogQgEAuE/hBI4niZgGo7Z1KX4Zbh3/8FxHTs7Ozs7O0/rePRo67gPoAQiEAhEEJBJ4DRJ8zRNx4Ikl0BCiORjixI4nbI0Dg4OxtMpSiACgUAgRDAYBfLjv+E4GQ6tJJCIIZfAozpKCTw6Okrepcm79Mnzw/F4etQHrq6uUAIRCATCfwiVhh5X7e7ults7OzvZxsXFxXSaDIdjpf5JFI6WOtE2KIFMNzK2r1+/TpJkd3f3yfPD6TQZj6dWpiGkwVeHh4cogQgEAhEEtCSwlL00TZ8+fZrpXzb2unXrllL/JMLWeBT4oY7pNJkmydOnT6fT5NGjrSyNx9MP2iCE6GyIQFeFEohAIBBBQKg0jx5tvX79end3d6eOTGayl03kEsgrGSiBym1QAt/XwT8LfPL8MB8OJsk0Sfb29t6rQAihN+i/TAHRjhkW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PWi3YsDZifBvEMVvDqzMjHkOzLMlTrovV1V1MC03Qzpq9wPQqIPdP2gUOgpmtKW4F8QMid723BB9OpJJBZNZ5KyXoUrb9qf9V4XoHMHswUcbbpAx5N/UvblsCMwSzWPcKcG1C8u1wP+NaFhVfvWWB2Ts4nNwbx7eLjs8kAevnNuzjunLYPHAI1XVPacsxFT1JUO+rCB9OZSWCyHpHR9unZxenZq/WIjLYa6p9MAunHYqzViy0uZ07fR7R4IxRoXeE/CPJID94ILe9Eah9fzg0o2b3+tICVE9aAtauL2nc6HvGksPaFsM5ZnaabMfXC26x87J/up7Pb+eLg/sVx17R94BCo6ZrSBgCEAsn5bgcfTKeQwMvF1cnpOZ0ej/KXOKL1l8xXRknkgOotEepnKDOTn22VHjLaq/Q137rUfxDmxY9oipqrjMGgiJ88F7M3jjswIFCneHdJ65KqNWquLiB6LSzsMgO9s3p/PrlRvfOdPquOmDIzey+8RLwJ5HQdx93S9oFDoKZrShuGTixtBT6YTiGBF5eLjydnLpIPcxNYou0umL1ttQQGXEr4MOFFm3F8xTisGG08WZQSeLE4/njqIvngAEu02QWzm6Btt45oDz6c1U0DAXJYNdp4sqgk8Pzi8uj4xEXywQGW6LcLS2DApYQPZ3XTQIAcVo02niwqCfzFGXxwgCVQAhE8fDirmwYC5LBqtPFkUc8O4wo+OMASKIEIHj6c1U0DAXJYNdp4sqgkEIFAIBCIZQWjyjUJdKr/CAQCgUB4BZRABAKBQKwoUAIRCAQCsaJACUQgEAjEiuL/Ae7DPoS330dyAAAAAElFTkSuQmCC" alt="" />
当在第三行加上筛选语句时,可以得到指定的某一行。例如:
WITH ReportCard
AS(
SELECT s.StuID,s.Name,s.EntranceTime,sc.CourseName,sc.Score FROM Student s,SltCourse sc WHERE s.stuid=sc.stuid AND s.StuID=''
)
SELECT * FROM ReportCard PIVOT(MAX(Score) FOR CourseName IN([电脑维修],[剪纸],[市场策划],[信息检索],[插花],[刺绣]) ) AS T
GO
结果。
aaarticlea/png;base64,iVBORw0K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X0kRZGqqeDVaWP84Xxzfip/9+e6p/Y0NIn+7+98R8Nc008jf/b4yZnq/bR+KHt6JvxiIkj2YPdMdVdJGbWa3e+sl3QUNCmyyobR56vloPf8Xlha3vyr85PK9C3tEike0qKvqXFTVz4Nopprt1LpmHJZC/XvLC2PcXPwE8e6Ds5E6S7wRlFZHuu4sL0zroikIZlk71c88LW9vL7GXja7U0I2Z6buDC9s64IpGHZZC/XvLC1vfx+2Z12exNCtucmLkzvrCsCaVg22cs1L+y/pSUvaLc3IWR7buLC9M66IpCGZZO9XPPC1vbyBgAK/UoQBEEUAtSPRdubuuETBEEQhDuQ7REEQRBLBNkeQRAEsUT8H/Mepj/ARl5wAAAAAElFTkSuQmCC" alt="" />
=======================================================================
动态版。先查询出要转为列的行数据,然后设置格式,再拼接字符串。
DECLARE @strCN NVARCHAR(100)
SELECT @strCN=ISNULL(@strCN+',','')+ QUOTENAME(CourseName) FROM SltCourse GROUP BY CourseName
--PRINT @DCN DECLARE @SqlStr NVARCHAR(1000)
SET @SqlStr='
WITH ReportCard
AS(
SELECT s.StuID,s.Name,s.EntranceTime,sc.CourseName,sc.Score FROM Student s,SltCourse sc WHERE s.stuid=sc.stuid
)
SELECT * FROM ReportCard PIVOT(MAX(Score) FOR CourseName IN('+@strCN+') ) AS T'
EXEC(@sqlstr)
GO
=======================================================================
题外话,关于PIVOT中聚合函数的使用。
我刚用TSQL不久,行转列都是汇总一些数值类型的数据,比方说,周一到周日的营业收入啦,数理化史地生成绩啦之类的。
这些数值类型的,选择聚合函数时,当然是需要什么选什么啦。
看例子。
USE tempdb
GO IF OBJECT_ID('AggregateFuncTest1','U') IS NOT NULL
DROP TABLE AggregateFuncTest1
GO CREATE TABLE AggregateFuncTest1
(
ID INT IDENTITY(1,1) PRIMARY KEY,
DayOfWeek NVARCHAR(3),
Income INT
)
GO INSERT INTO AggregateFuncTest1(DayOfWeek,Income) VALUES('星期一',100)
INSERT INTO AggregateFuncTest1(DayOfWeek,Income) VALUES('星期二',200)
INSERT INTO AggregateFuncTest1(DayOfWeek,Income) VALUES('星期三',300)
INSERT INTO AggregateFuncTest1(DayOfWeek,Income) VALUES('星期四',400)
INSERT INTO AggregateFuncTest1(DayOfWeek,Income) VALUES('星期五',500)
INSERT INTO AggregateFuncTest1(DayOfWeek,Income) VALUES('星期六',600)
INSERT INTO AggregateFuncTest1(DayOfWeek,Income) VALUES('星期日',700)
GO
----------------------------------------------------------------------
SELECT DayOfWeek,Income FROM AggregateFuncTest1
GO
----------------------------------------------------------------------
WITH Rst AS
(
SELECT DayOfWeek,Income FROM AggregateFuncTest1
)
SELECT [星期一],[星期二],[星期三],[星期四],[星期五],[星期六],[星期日] FROM Rst
PIVOT(AVG(Income) FOR DayOfWeek IN ([星期一],[星期二],[星期三],[星期四],[星期五],[星期六],[星期日])) AS T
GO
----------------------------------------------------------------------
INSERT INTO AggregateFuncTest1(DayOfWeek,Income) VALUES('星期一',1000)
GO WITH Rst AS
(
SELECT DayOfWeek,Income FROM AggregateFuncTest1
)
SELECT [星期一],[星期二],[星期三],[星期四],[星期五],[星期六],[星期日] FROM Rst
PIVOT(AVG(Income) FOR DayOfWeek IN ([星期一],[星期二],[星期三],[星期四],[星期五],[星期六],[星期日])) AS T
GO
----------------------------------------------------------------------
DROP TABLE AggregateFuncTest1
结果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAYMAAAEpCAIAAAD+gdnNAAAezUlEQVR4nO2dzW7bSLqG6246jJZa9UUEjVDQena6ggYaPbIBbQNkmwBCA4302HIAHZxNzg24F72JZXvOQDMZzIGnO510OuNOENmxLVk/0FlQpIqsHxZFFuuj/D4oGOUyWXyryHrzkaK+sBEAALiGPXr8pKLF9dQBAAqDPXr8ZFlB4EQAbBMVdiL6xfXJBaAyVNiJ5rSBEwFgjs6JWt2BtNRbyVKmBwU8evzkMs7h4eHh4eGLOM+e7V+6AE4EQCZSnKg7WK5Kd9lSFL0TMcY0vxboRN1uUsb5+Xmr24UTAUCfbDGRGA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Pe+/knSUv5nZ9zHHSGuP+8YEvw4j9NG5LMzbpbC+zTXkriFTOrExQ/kVJLEiRQJZHsPmb+nTS6ZWjQxUfSuUBQQaVrKeZ+Ifyklevbp/LWU6HDRdeFcUgB/qTqXRHGWiL1PtDpe/EC03ieyl0AW71gbAkkmQJIJVZEkcyJrCWTxvTNDqnL1uAWSTKiKJIkT2cs1iZjIEEgyAZJMqIokiRPZyxeJmMiQqlw9boEkE6oiSfqOtS0QExkCSSZAkglVkVR29tiR60Sxqp8AgHKQ2lPSiQr3PwAASAVOBABwD5wIAOAeOBEAwD1wIgCAe+BEAAD3wIkAAO6BEwEA3AMnAgC4B04EAHBP0olQUFBQyi8xJ3L9fRQAwB0FTgQAcA+cCADgnpgTAQCAE+BEAAD3/D/+88gN457S7AAAAABJRU5ErkJggg==" alt="" />
只要看一看、运行一下就会发现,聚合函数的选择是根据需要来的。当我有2个星期一的入账时,由AVG函数的来的结果是不同的。
那么,求最大就是用Max,最小用Min,求和就是Sum,等等等等。没啥说的。
如果数据是字符类型呢?
再看例子。例如,办公室值日表。
USE tempdb
GO IF OBJECT_ID('AggregateFuncTest2','U') IS NOT NULL
DROP TABLE AggregateFuncTest2
GO CREATE TABLE AggregateFuncTest2
(
ID INT IDENTITY(1,1) PRIMARY KEY,
DayOfWeek NVARCHAR(3),
Name NVARCHAR(10)
)
GO INSERT INTO AggregateFuncTest2(DayOfWeek,Name) VALUES('星期一','Jack')
INSERT INTO AggregateFuncTest2(DayOfWeek,Name) VALUES('星期二','Lucy')
INSERT INTO AggregateFuncTest2(DayOfWeek,Name) VALUES('星期三','Lily')
INSERT INTO AggregateFuncTest2(DayOfWeek,Name) VALUES('星期四','Bob')
INSERT INTO AggregateFuncTest2(DayOfWeek,Name) VALUES('星期五','Angela')
INSERT INTO AggregateFuncTest2(DayOfWeek,Name) VALUES('星期六','Bella')
INSERT INTO AggregateFuncTest2(DayOfWeek,Name) VALUES('星期日','')
GO
------------------------------------------------------------------------
SELECT DayOfWeek,Name FROM AggregateFuncTest2
GO WITH Rst AS
(SELECT DayOfWeek,Name FROM AggregateFuncTest2)
SELECT * FROM Rst PIVOT (MAX(Name) FOR DayOfWeek IN ([星期一],[星期二],[星期三],[星期四],[星期五],[星期六],[星期日]) ) AS PVT
------------------------------------------------------------------------
DROP TABLE AggregateFuncTest2
结果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAYsAAAAyCAIAAAAiMGDVAAAG1ElEQVR4nO2czW7bOBSF+TY14GcJiiTQmwQYZOf3CBAg6KQDqOiqT5BFNrP2bKdNm6btYhDHza8sQ7OQbdEieXVJiRJDnwOiVWiJ/HxJn14yLkVRFEVRfPj4qYAgCApMovzrw8dPKCgoKKGVlUPNIAiCwhMcCoKgcAWHgiAoXGEfCgUFJdxSOZTXDfn5fO61fTcFSAUkjoDEUYBIhT0VHCosAYkjIHEUIFIRrEPd3d15bd9NAVIBiSMgcRQgUmFPNaRDCbPke2oXcVMBCUiErLADjJKJitDADqW9mQ6WNqbRUHlF4szOcJAIKn9IbjxekZht9ozkFiUTFaGeHGo2m2n6bnqT2no1fGFSuQ1hgIHyHaXGi56R5AbJwAyAZEXVz8DZ4mmpCPXkULe3t5q+Xe2cfvxVU+0akht2P1GyitvODhyzfZqKUOVQi0X+9Jz5KHm+/PHzl9o+Yefae0w10VDtIBLnYqgo8ePmFYloRIvd88BZBa2kcnSobJE/Pr34KHm+/H7zg25fCEHXExfRUO0gkgN2D0haYeA4UeIEKjiHWi6X199v1PblN0MEq/bOaxdEpOh4eaUKMFDExBoQiXMxyMDRPw6O1DjNehs4on0TlatDZYuHx2cfJV8uv13fqO1rPy2l5HvUC/W2OKh2EKm8qBHS2P1Eif4RSKbeOVSvw6E44dYGsTFGr5dqB5HUzyHdbG9R4jTYJ5Lp8QEHzmrUZCpHh3rJFvcPTz5Kni+vvl5r21fDIYSgW9vc0Hjnq6PaQSTiEdNLPURJfYpuZxAkmmqogeMEytGhnl+y3/ePPkqeL79cfaPbF0LQ9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+rPE2QkoRaoWtrqmRQHXdrJDpKW93Zd0orQocyHAR8vi8O3pGHhDYW4z7U6X65oitTp4s/RlXyK++ICyHqO+Xlza12o6gcSk7D5cR8PNbulJdqv1HN285YY0ifSB975FZI9U3oThcsPCQlBmXvahKnodqM5tZOuWYTyQ7JECVtd/ad0orQofwdBLwb3ynv5msZQ3VtUhwTXa/uwhZzlLpTa4fydhBw/P8vr6Nllsus8rjCK4pYJrok+bf5nYUtuih5UVuH8ndm6G7kUB0ISBwBiaM4kLYcyt+5n/HnUB0pjlnlW0DiKA6k2nfKfQk5FFNA4ghIHMWB1N8pwLOhD/w1/QlBUD9yMLXKoRwehiAI8io4FARB4apyKBQUFJTQysqhhl6fQhAEaQSHgiAoXMGhIAgKV/8D8lN/mhkjDDUAAAAASUVORK5CYII=" alt="" />
单个的数据可以用Max等函数来凑合使用,因为这些函数可以接受字符类型的参数。
但如果数据多了,不止一个的时候呢?例如,又来了一个同事,于是周六多了一个人,那就顺便大扫除吧。
INSERT INTO AggregateFuncTest2(DayOfWeek,Name) VALUES('星期六','Alice')
GO
WITH Rst AS
(SELECT DayOfWeek,Name FROM AggregateFuncTest2)
SELECT * FROM Rst PIVOT (MAX(Name) FOR DayOfWeek IN ([星期一],[星期二],[星期三],[星期四],[星期五],[星期六],[星期日]) ) AS PVT
GO
结果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAX8AAAApCAIAAABShr5uAAAGe0lEQVR4nO2cz27bOBDG+TY14GcJCifQmwRY5Ob3CBAg6GYX0GJP+wQ55LJn77Vt2jTtHhZx3PxxZBnag2SJFsnRkNKQkjwfiEKhJfKnofRlyLgUWZYtWSwWy7tElmV//PkXFy5cuHguhftklFqtVqTtu6mHVIyEESNh1EOkTKFi9+mRGAkjRsKoh0hZEPd5fHwkbd9NPaRiJIwYCaMeImUKVTD3EWbJ59QOxk3FSIwEyAq7h1FSqUK6j/ZkOBDaeI2GihQJ8+T1BwmgokNy4yFFQrbpGcktSiqVD/dZ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qKINuRsp25lMClSGSCeTZUB27umohj3ETvWuUpO7sO4U1MPcxbG/427E4+RXcSK2xWOc+Rc3fpxPZhianF3n95ZGYnN74yn007iM/U3EkpvO4rGiVKrrMvBbddO2CVF/RqFCKo8V8KvZrSJFK89kbKMl0amNX1eyCa0pDmhITyyjpurPvFBa7T+U+qHWfm7N35UhNzq61rpR/+v6y/czLPffRPRbFq9/OAdzWfTrp2gWpNqeAA9Wd/Zjfq713XagAphoh9i6L1SSlYSHbLkra7jSZUavV86G5j357w6tjcfLBdXO8vOhzH9l0ahlNXqN3n9n5Xk4UIvfR/lKKIxHF7dYbXFedu+jaBal6rxbzqfTSqoTqLRAhVf6GRlJtsThHmsi2yn2UKGm7s+8UFrtP5T663GdnNxczUbjP7Dz/qKgxzbxKG/KS+8hJRfFsKDOvwgPa/qnX9W9eHXTtgiSvaOwmWsrMS1psIZ55aYIURdCsWVtTJXHquFsjwVHa686+U1gDcx/D5qpXM3H8Adx4sbEY130uZvksK095rn+ZVAmpvLoshKivOucnt1r9gXIfOTWWk+XpVLvqnKv9oi9u+WCHIb1tFOvNVkj1Bd1OJxE4JCUGee9q8qWhKkdzb9VZs2hjh2SIkrY7+05hDcx96DZXPYzvOnfz1YRQXZs0iIfYUd2FbcxR6k6g+5Btrjr+/+fV0dTH5YkhnHVl2UAeYhvJf9HuLGyjixKJIPeh24fxMHKfDsRIGDESRoNAqtyHbi/F8ec+HWkQT0xwMRJGg0CSv+tMJc59kGIkjBgJo0EgedpZdRl6E1XTvywWy49UPyrcx4PzsVgslqz/AesQjihytWxLAAAAAElFTkSuQmCC" alt="" />
Alice居然不用值日,这怎么行!怎能不给表现的机会?女汉子能顶一个天!
这种情况只能自己专门写一个聚合函数来拼接字符串了。(当然可以先拼接好字符串,再行转列。)
=======================================================================
用户自定义聚合函数好复杂,不写了。(其实是我还没搞懂这些呢,C#无爱。)
SQL Server 2008 R2——PIVOT 行转列 以及聚合函数的选择的更多相关文章
- SQL Server:使用 PIVOT 行转列和 UNPIVOT 列转行
ylbtech-SQL Server:使用 PIVOT 行转列和 UNPIVOT 列转行 可以使用 PIVOT 和 UNPIVOT 关系运算符将表值表达式更改为另一个表.PIVOT 通过将表达式某一列 ...
- SQL Server中使用PIVOT行转列
使用PIVOT行转列 1.建表及插入数据 USE [AdventureDB] GO /****** Object: Table [dbo].[Score] Script Date: 11/25/201 ...
- sql server 2008 数据的行转列
create table tb(id int, value varchar(10)) insert into tb values(1,'aa') insert into tb values(1,'bb ...
- SQL Server 2008 R2——使用计算列为表创建自定义的自增列
=================================版权声明================================= 版权声明:原创文章 谢绝转载 请通过右侧公告中的“联系邮 ...
- SQL Server 2008 R2中,变表的右键弹出菜单中的“选择前1000行”为“选择所有行”
原文:SQL Server 2008 R2中,变表的右键弹出菜单中的"选择前1000行"为"选择所有行" 从SQL Server 2008开始,微软为了提高查询 ...
- SQL Server 2008 R2 使用 PIVOT 错误
SQL Server 2008 R2 使用 PIVOT 错误! 'PIVOT' 附近有语法错误.您可能需要将当前数据库的兼容级别设置为更高的值,以启用此功能. 有关 ALTER DATABASE 的 ...
- SQL Server 2008 R2不支持limit(限制行数)
SQL Server 2008 R2不支持limit 可用:select top 3 * from Websites2 MySQL 语法 SELECT *FROM PersonsLIMIT 5; Or ...
- [转帖]微软 SQL Server 2008/R2 停止支持
微软 SQL Server 2008/R2 停止支持 微软停止支持 SQLSERVER 2008R2 https://t.cj.sina.com.cn/articles/view/3172142827 ...
- SQL Server 2008 R2——学习/练习/错误/总结/搜集
==================================声明================================== 本文原创,转载在正文中显要的注明作者和出处,并保证文章的完 ...
随机推荐
- go语言操作mysql范例(增删查改)
http://blog.csdn.net/jesseyoung/article/details/40398321 go语言连接mysql简介 go官方仅提供了database package,d ...
- android 查找某个特定文件后缀名
private void queryFiles(){ String[] projection = new String[] { MediaStore.Files.FileColumns._ID, Me ...
- android 网络通讯
//get方式请求网络数据 String urlPath="http://192.168.98.112:8080/CloudMusicPlayer/fragment1_1_lv2/json/ ...
- 设置文件为源文件(和src一样)
点击project->properties->java build path->source
- 搭建自己的ngrok服务(国内直接可用http://qydev.com/#)
ngrok 服务可以分配给你一个域名让你本地的web项目提供给外网访问, 特别适合向别人展示你本机的web demo 以及调试一些远程的API (比如微信公众号,企业号的开发) ngrok的官方服务可 ...
- 一:【nopcommerce系列】Nop整体架构的简单介绍,在看nop代码之前,你需要懂哪些东西
首先,我看的是Nop 3.80,最新版 百度资料很多,Nop用到的主要的技术有: 1.Mvc,最新版用的是 5.2.3.0 2.entity framework 3.autofac 4.插件化 5.( ...
- 通过ABAP的标准功能,寻找源代码中的字符串
程序名称: *ABAP_SOURCE_SCAN 查找 作用:通过这个标准程序,可以在系统的源代码中进行字符串的查找!
- 操作系统开发系列—11.ELF格式 ●
ELF文件的结构如下图所示: ELF文件由4部分组成,分别是ELF头(ELF header).程序头表(Program header table).节(Sections)和节头表(Section he ...
- Android终止线程的方法
线程对象属于一次性消耗品,一般线程执行完run方法之后,线程就正常结束了,线程结束之后就报废了,不能再次start,只能新建一个线程对象.但有时run方法是永远不会结束的.例如在程序中使用线程进行So ...
- iOS 图片加载导致内存警告
虽然UITableView和UICollectionView都有cell复用机制,但是如果利用SDWebImage加载的图片本身过大且cell复用池中的个数比较多(cell的Size越小,复用池中的c ...