1.插入数据:
.插入单行:
INSERT INTO `表名` ( `列名1`, `列名2`, `列名3`) VALUES
(`值1`, `值2`, `值3` );
.插入多行:
INSERT INTO `表名` ( `列名1`, `列名2`, `列名3`) VALUES
(`值1`, `值2`, `值3` ),
(`值1`, `值2`, `值3` ),
...
(`值1`, `值2`, `值3` );
2.删除数据:
DELETE FROM `表名` WHERE 语句 // '列名'='' or '列名' like '%xxx'
3.更新数据:
UPDATE table_name SET field1=new-value1, field2=new-value2
[WHERE Clause]
4.查询:
.普通查询
SELECT * FROM `表名` [WHERE Clause] LIMIT [查询多少个] OFFSET [偏移];
.分组查询记录数并按某个字段降序排列
SELECT 列名,COUNT(*) FROM `表名` GROUP BY 列名 ORDER BY 列名 DESC ;
.联合查询(UNION)
说明:用于将两个表中相同的列名的数据拼接起来,UNION默认是 DISTINCT,查询结果重复数据只会出现一次。ALL:重复结果会全部展示出来
SELECT expression1, expression2, ... expression_n FROM tables
[WHERE conditions]
UNION [ALL | DISTINCT]
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions];
.连接查询
1.内连接(INNER JOIN)
获取两个表中字段匹配关系的记录
示例:
SELECT a.aid,a.reward_num,b.nickname FROM `dream_task_reward_record` a INNER JOIN `dream_accounts` b ON a.aid=b.id;
//等价于
SELECT a.aid,a.reward_num,b.nickname FROM `dream_task_reward_record` a ,`dream_accounts` b WHERE a.aid=b.id;
自己手动测试(工具:PHPAdmin):
单独查询:dream_task_reward_record表
SELECT aid,reward_num FROM `dream_task_reward_record`;
结果:
单独查询:dream_accounts表
SELECT id,nickname FROM `dream_accounts`;
结果:
内联查询上面两张表:
SELECT a.aid,a.reward_num,b.nickname FROM `dream_task_reward_record` a INNER JOIN `dream_accounts` b ON a.aid=b.id;
根据网上的关系图
2.左连接(LEFT JOIN)
示例:
SELECT a.aid,a.reward_num,b.nickname FROM `dream_task_reward_record` a LEFT JOIN `dream_accounts` b ON a.aid=b.id;
3.右连接(RIGHT JOIN)
示例:
SELECT a.aid,a.reward_num,b.nickname FROM `dream_task_reward_record` a RIGHT JOIN `dream_accounts` b ON a.aid=b.id;