数据库对其mysql -u root -p --default-character-set=latin1
set names gbk;
5.1 插入数据
insert into 表名(col1,col2,……) values(val1,val2……); -- 插入指定列
insert into 表名 values (,,,,); -- 插入所有列
insert into 表名 values -- 一次插入多行
(val1,val2……),
(val1,val2……),
(val1,val2……);
5.3修改数据
update tablename
set
col1=newval1,
col2=newval2,
...
...
colN=newvalN
where 条件;
5.4,删除数据 delete from tablenaeme where 条件;
5.5, select 查询
(1) 条件查询 where a. 条件表达式的意义,表达式为真,则该行取出
b. 比较运算符 = ,!=,< > <= >=
c. like , not like ('%'匹配任意多个字符,'_'匹配任意单个字符)
in , not in , between and
d. is null , is not null
(2) 分组 group by
一般要配合5个聚合函数使用:max,min,sum,avg,count
(3) 筛选 having
(4) 排序 order by
(5) 限制 limit
6: 连接查询
6.1, 左连接
.. left join .. on
table A left join table B on tableA.col1 = tableB.col2 ;
例句:
select 列名 from table A left join table B on tableA.col1 = tableB.col2
2. 右链接: right join
3. 内连接: inner join
左右连接都是以在左边的表的数据为准,沿着左表查右表.
内连接是以两张表都有的共同部分数据为准,也就是左右连接的数据之交集.
7 子查询
where 型子查询:内层sql的返回值在where后作为条件表达式的一部分
例句: select * from tableA where colA = (select colB from tableB where ...);
from 型子查询:内层sql查询结果,作为一张表,供外层的sql语句再次查询
例句:select * from (select * from ...) as tableName where ....
数据库连接
第一种:
[email protected]_connect('localhost','root','admin');
mysql_query('use 表名',$con);
mysql_query('set names utf8');
查询时
直接用
$res=mysql_query($sql);
执行插入:
if(!mysql_query($res)){
echo '失败'
}
select * from 表
则先用数组存起来
$cat=array();
$rs=mysql_query($sql);
while($row=mysql_fetch_assoc($rs)){
$cat[]=$row;
}
var_dump($cat);//此时的数为二维数组
第二种:
$host = 'locahost';
//url 不要加端口,要单独指定,我就栽在这上面了
$db = new mysqli($host,$user,$paddwd,$database,$port);
//连接数据库//或者这样也可以
$db = mysqli_connect($host,$user,$passwd,$database,$port);
//连接数据库$db->set_charset('utf8');
//设置查询结果编码$result = $db->query($sql);
//得到查询结果w
hile($row = $tempResult->fetch_array())
{//遍历结果echo$row['post_title'];
}$db->close();//关闭连接
第三种:
删$result是删除的条数;