Mysql 交叉查询
CREATE TABLE `taa` (
`year` varchar(4) DEFAULT NULL,
`month` varchar(2) DEFAULT NULL,
`amount` double DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf
"year","month",amount
"1991","1",1.1
"1991","2",1.2
"1991","3",1.3
"1991","4",1.4
"1992","1",2.1
"1992","2",2.2
"1992","3",2.3
"1992","4",2.4
1.select a.year,a.m1,a.m2,b.m3,b.m4 from
(select year,
sum(if(month=1,amount,0)) as m1,
sum(if(MONTH=2,amount,0)) AS m2
from taa
group by year) a,
(SELECT year,
SUM(IF(MONTH=3,amount,0)) AS m3,
SUM(IF(MONTH=4,amount,0)) AS m4
from taa
GROUP BY YEAR) b
where a.year=b.year;
2.select year,
max((case month when 1 then amount end)) as m1,
max((CASE MONTH WHEN 2 THEN amount END)) AS m2,
max((CASE MONTH WHEN 3 THEN amount END)) AS m3,
max((CASE MONTH WHEN 4 THEN amount END)) AS m4
from taa
group by year;
相关文档:
要安装 MySQL,可以在终端提示符后运行下列命令:
sudo apt-get install mysql-server
sudo apt-get install mysql-client
sudo apt-get install php5-mysql
// 安装php5-mysql 是将php和mysql连接起来
一旦安装完成,MySQL 服务器应该自动启动。您可以在终端提示符后运行以下命令来检查 MySQL 服务器是否正在运行: ......
新使用MySQL,说起来是个简单的事情,但是却费了些周折:
1、登陆服务器端,进入命令行,windows cmd;
2、设置用户、密码让指定的IP访问:mysql -u root -p 或安装的快捷方式进入:MySQL Command Line Client,使用grant命令:
grant 权限1,权限2,…权限n on 数据库名称.表名称 to 用户名@用户地址 identified ......
自从认识mysql的那天起就知道varchar的长度限制为255,不过现在这种情况已经改变了:
Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 65,535. The effective maximum length of a VARCHAR is subject to the maximum row size (65,535 by ......
格式为2008-06-16
查询出当天数据:
SELECT * from `table` WHERE date(时间字段) = curdate();
查询出当月字段:
SELECT *
from `table`
WHERE month( 时间字段) = month( now( ) ) ;
时间格式为1219876…… UNIX时间,只要应用“from_UNIXTIME( )”函数
例如查询当月:
SELECT ......
很多朋友都有过这样的经历,将mysql升级到4.1(或以上)版本后,旧的程序从数据库读出来的都变成乱码了.这个问题网上很多这方面的讨论,其实手册上已经有关于这方面的详细说明,
以下内容摘自mysql手册,
产生这个问题的原因是:
MySQL 4.1.x开始支持以下这些事情
· 使 ......