mysql 安装
yum -y install mysql-server
yum -y install php-mysql
vi /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
default-character-set=utf8
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
chkconfig mysqld on
chkconfig --list mysqld
/etc/rc.d/init.d/mysqld start 初始化mysql
mysqladmin -u root -p password
delete from mysql.user where user=''; 删除匿名用户
flush privileges; ← 刷新,使以上操作生效
mysql
1. distinct
如果包含NULL值, 只会显示一个NULL
SELECT DISTINCT state, city FROM customers 过滤多个列的唯一组合
If you use the GROUP BY clause in the SELECT statement without using aggregate functions, the GROUP BY clause behaves like the DISTINCT clause.
2. limit
SELECT column1,column2,...FROM table LIMIT offset , count;
SELECT column1,column2,...FROM table LIMIT count;
3. union
(SELECT customerNumber id,contactLastname name FROM customers) UNION (SELECT employeeNumber id,firstname name FROM employees) ORDER BY name,id
(SELECT customerNumber, contactLastname FROM customers) UNION (SELECT employeeNumber,firstname FROM employees) ORDER BY 2, 1 //order by column postition
当使用 UNION 时,MySQL 会把结果集中重复的记录删掉,而使用 UNION ALL ,MySQL 会把所有的记录返回,且效率高于 UNION。
重复记录是指查询中各个字段完全重复的记录,如上例,若 title 一样但 id 号不一样算作不同记录。
第一个 SELECT 语句中被使用的字段名称也被用于结果的字段名称,如上例的 aid。
各 SELECT 语句字段名称可以不同,但字段属性必须一致。
4. join
INNER JOIN(内连接):取得两个表中存在连接匹配关系的记录。
SELECT article.aid,article.title,user.username FROM article INNER JOIN user ON article.uid = user.uid 等同于
SELECT article.aid,article.title,user.username FROM article,user WHERE article.uid = user.uid
LEFT JOIN(左连接):取得左表(table1)完全记录,即是右表(table2)并无对应匹配记录。
RIGHT JOIN(右连接):与 LEFT JOIN 相反,取得右表(table2)完全记录,即是左表(table1)并无匹配对应记录。
5. having
在SQL HAVING子句用于限制有条件的一个SQL语句的输出,通过一个SQL总在你列的SELECT列表中使用的功能。
SELECT Employee, SUM (Hours) FROM EmployeeHours GROUP BY Employee HAVING SUM (Hours) > 24
yum -y install mysql-server
yum -y install php-mysql
vi /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
default-character-set=utf8
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
chkconfig mysqld on
chkconfig --list mysqld
/etc/rc.d/init.d/mysqld start 初始化mysql
mysqladmin -u root -p password
delete from mysql.user where user=''; 删除匿名用户
flush privileges; ← 刷新,使以上操作生效
mysql
1. distinct
如果包含NULL值, 只会显示一个NULL
SELECT DISTINCT state, city FROM customers 过滤多个列的唯一组合
If you use the GROUP BY clause in the SELECT statement without using aggregate functions, the GROUP BY clause behaves like the DISTINCT clause.
2. limit
SELECT column1,column2,...FROM table LIMIT offset , count;
SELECT column1,column2,...FROM table LIMIT count;
3. union
(SELECT customerNumber id,contactLastname name FROM customers) UNION (SELECT employeeNumber id,firstname name FROM employees) ORDER BY name,id
(SELECT customerNumber, contactLastname FROM customers) UNION (SELECT employeeNumber,firstname FROM employees) ORDER BY 2, 1 //order by column postition
当使用 UNION 时,MySQL 会把结果集中重复的记录删掉,而使用 UNION ALL ,MySQL 会把所有的记录返回,且效率高于 UNION。
重复记录是指查询中各个字段完全重复的记录,如上例,若 title 一样但 id 号不一样算作不同记录。
第一个 SELECT 语句中被使用的字段名称也被用于结果的字段名称,如上例的 aid。
各 SELECT 语句字段名称可以不同,但字段属性必须一致。
4. join
INNER JOIN(内连接):取得两个表中存在连接匹配关系的记录。
SELECT article.aid,article.title,user.username FROM article INNER JOIN user ON article.uid = user.uid 等同于
SELECT article.aid,article.title,user.username FROM article,user WHERE article.uid = user.uid
LEFT JOIN(左连接):取得左表(table1)完全记录,即是右表(table2)并无对应匹配记录。
RIGHT JOIN(右连接):与 LEFT JOIN 相反,取得右表(table2)完全记录,即是左表(table1)并无匹配对应记录。
5. having
在SQL HAVING子句用于限制有条件的一个SQL语句的输出,通过一个SQL总在你列的SELECT列表中使用的功能。
SELECT Employee, SUM (Hours) FROM EmployeeHours GROUP BY Employee HAVING SUM (Hours) > 24