sql中遇到的问题

问题:

  1. 从数据库获得的数据是资源型的怎么处理?

 

  1. 如果查询记录为0,如何处理?

使用mysql_num_rows()函数,获取查询的记录数,然后进行处理。

  1. 数据查询

SQL语句分为两类

1、数据查询语句:select   show

2、数据操作语句:insert 、update、delete、drop

只有数据查询语句才有记录集返回

数据查询语句执行

              成功返回       记录集

              失败返回       FALSE

数据操作语句

              成功返回TRUE

              失败返回false

  1. 切记在php当中””双引号会翻译其中的变量,’’单引号只是简单的字符串。
  2. Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in E:\wamp\www\fenye.php on line 55

这种错误,很有可能是因为sql语句的错误,是否关键字或者表名出错。

6、排序

select * from stu order by score desc limit 3;

  1. sql语句执行不成功

有可能是表名或者库的名字没有加反引号``。

  1. Js中通过number.tofixed(num),将number数字转化成字符串并保留num位小数
  2. 在mysql-front软件中不支持show create table tn_name \G命令
  3. 默认存储引擎为INNODB
  4. 自动增长是从1开始的不存在0
  5. 插入数据时,主键冲突?

使用duplicate(重复)

insert into teacher_class (id, t_name, c_name) values

(13, '杨露婵', '太极')

on duplicate key update

t_name='杨露婵', c_name='太极';

  1. 请空表,重新插入主键从1开始?

Delete from tb_name;//主键不会从1开始

Truncate tb_name;

  1. 触发器insert,update,delete事件?

事件insert 没有old,不能使用old

事件delete 没有new,不能使用new

事件update 两个都有

  1. 触发多个事件?

多个更新操作中使用分号隔开,此时需要修改语句的结束符,使用delimiter 来修改,例如delimiter $$。当创建完触发器后,切记要将语句结束符,改回来,delimiter ;。

  1. 字符串连接

Php中    ‘.’符号

Mysql中 concat(string1,string2)函数

Js中string.concat(string2),+号也是连接

  1. select into

注意,select into @var 要求,只能返回一行。如果返回多行,会语法错误,或者只将最后一行的数据,注入到变量内。

 

  1. Substring、Right、left字符串截取函数?

Left(str,length)从左侧开始截取length长度

Substring(str,pos);pos截取开始的位置,此函数表中下标从1开始

Substring(str,pos,length)lenggth截取的长度

  1. lpad填充函数

Lpad(str,len,padstr)

指定str字符串的长度为len,如果不够,就使用padstr字符串进行填充

  1. 查看当前数据库的相关编码集。

show variables like 'character%';

修改数据库的编码

alter database db_name  character set utf8;

 

set character_set_server=utf8;

  1. 创建函数时注意返回是returns。

create function sno(c_id int) returns char(10)

  1. 创建函数时出现14181错误,ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)

解决方法:MySQL中创建函数时出现这种错误的解决方法:

show variables like 'log_bin_trust_function_creators';
       set global log_bin_trust_function_creators=TRUE;

 DETERMINISTIC  确定的。

  1. mysql-front数据库客户端设置结束符出错?

Mysql-front不支持设置结束符号,delimiter $$

  1. Duplicate entry '' for key 'PRIMARY'

重复插入。

  1. 怎么查不同条件的信息?

使用union函数:

使用union查询时会有一些问题,比如同时分别查询两条信息的内容,union会把重复的内容过滤掉。此时使用union all,则不会将重复内容去掉。

  1. 内连接语法格式?

Select (查询内容【可以是不同表的不同内容】) from 表1 inner join 表2 on 表1.id(与表2相同的字段)=表2.id

内链接的处理:

内连接,在连接时,是可以省略连接条件的。意味着,所有的

左表的数据,都要与右表的记录做一个连接。

共存在 MXN个连接

这种连接,就称之为,交叉连接,或者笛卡尔积。

有条件的内连接:

会在连接时过滤 非法的连接。

 

where的写法:数据过滤,理解上,数据安装交叉连接连接完成后,再做数据过滤。

on的写法:在连接时,就对数据进行判断。

Using的写法。Using 要求,负责连接的两个实体之间的字段名称,一致。 

建议是,在有同名字段时,使用using ,而在通用条件时,使用on。

在数据过滤时(不是指的连接过滤) 使用where。 

注意,查询条件,与外连接通用(外连接,不能使用where作为连接条件);

  1. 外链接语法格式?

左外链接:Select (查询内容【可以是不同表的不同内容】) from 表1 left [outer] join 表2 on 表1.id(与表2相同的字段)=表2.id

Using:

会去掉结果中的 重复字段,并放在列前。

  1. 自然连接语法格式?

Select (查询内容【可以是不同表的不同内容】) from 表1 natural join 表2;

自然连接分为

Natural join 自然内连接

Natural left/right join 自然外(左/右)链接

查询示例比较:

select * from one left join two using (public_field); 

select * from one natural right join two;

select * from one right join two using (public_field);

  1. forbidden,you don’t have permisssion to access /on this server?

没有权限

DocumentRoot "实际物理路径”

<Directory ‘实际物理路径’>

Options Indexes

Order Deny,allow

Allow from all

DirectoryIndex index.html //设置首页

<Directory>

  1. 数据库操作各种语句返回值整理?

增删改的返回值,成功为>0的整数,失败为false.

die终止语句

执行die()语句后,后面的语句将不再执行,包括html代码。

 

@ 屏蔽错误信息

在数据库连接出错时,浏览器会指出错误信息,为防止重要信息泄露,我们不需要让浏览者看到错误信息,所以需要屏蔽掉。需要在connect()函数前加@号即可。

 

网页跳转

Html中:使用<a href=’地址’>标记内进行页面之间的跳转

Js中:location.href=’地址’

Php中:header(location:’地址’);

 

连接数据库的几点

Mysql -hlocalhost:80 -uroot -p -P3306

 

Modify 修改,被修饰,改变,减轻

 

  1. 连接数据库mysql_connect(‘localhost’,’root’,’’)
  2. 选择数据库mysql_query(‘use db_name’),mysql_select_db(‘db_name’)
  3. 设置字符编码mysql_query(‘set names utf8’)
  4. 查询数据
  5. 关闭资源,释放连接

Mysql_close($link);

  1. mysql_num_rows();返回查询数据数

$n1=mysql_num_rows(结果集);//获得该结果集的数据行数

$n2=mysql_num_fields(结果集);//获得该结果集的数据列数

$name=mysql_field_name(结果集,$i);//获得结果集的第i个字段的名字,i从0开始

 

对象分类

BOM对象,浏览器鼓组件对象,包括window、document、location、history....

DOM对象,文档对象,包括所有HTML标记,每一个HTML标记都是一个对象。

校对规则

Ci不区分大小写

Cs区分大小写

_bin编码比较

 

Show collation;//显示校对规则

字符集_地区名_比较规则(ci,cs,bin)不区分,区分,字节比较

Show collation like ‘gbk%’;

 数据查询

In 是范围

=     是比较

连接

例子:  

 

-------------------------------------------------

  a表     id   name     b表     id   job   parent_id  

              1   张3                   1     23     1  

              2   李四                 2     34     2  

              3   王武                 3     34     4      

  a.id同parent_id   存在关系  

 

--------------------------------------------------   

 1) 内连接  

  select   a.*,b.*   from   a   inner   join   b     on   a.id=b.parent_id      

  结果是    

  1   张3                   1     23     1  

  2   李四                  2     34     2  

   

  2)左连接  

  select   a.*,b.*   from   a   left   join   b     on   a.id=b.parent_id      

  结果是    

  1   张3                   1     23     1  

  2   李四                  2     34     2  

  3   王武                  null  

 

 

 3) 右连接  

  select   a.*,b.*   from   a   right   join   b     on   a.id=b.parent_id      

  结果是    

  1   张3                   1     23     1  

  2   李四                  2     34     2  

  null                       3     34     4  

   

 4) 完全连接  

  select   a.*,b.*   from   a   full   join   b     on   a.id=b.parent_id  

 

  结果是    

  1   张3                  1     23     1  

  2   李四                 2     34     2  

  null                   3     34     4  

  3   王武                 null

 

连接规则

Select (查询)tb1.*,tb2.*(信息) from(从) tb1(表1) right join(连接方式) tb2(表2) on tb1.id=tb2.pid(连接规则);

数据库写文件

Select *into outfile ‘路径’ from tb;

//二进制保存

select * into dumpfile 'e:/amp/six' from teacher_class where t_name = '韩信' limit 1;

 

 

 

 

 

 

 

 

 

 

猜你喜欢

转载自blog.csdn.net/benben0729/article/details/81295995