mysql数据库自动插入代码(mysql手动添加数据)

发布时间:2022-11-15

本文目录一览:

  1. php+mysql写入数据库的一段代码的写法,在线等!
  2. 如何自动批量往mysql数据库中插入数据
  3. mysql数据库表的插入修改,求好的方法和代码
  4. Mysql数据库中,设置id为自动增加,向数据库中插入数据时,SQL语句怎么写?

php+mysql写入数据库的一段代码的写法,在线等!

<?php
$url = ""; //连接地址
$user = ""; //用户名
$pass = ""; //用户密码
$dbname = "shuzi"; //数据库名
$con = mysql_connect($url, $user, $pass);
if(!$con) {
    echo "连接错误";
    return;
}
$qi = $_POST["qi"]; //取表单值,通过名字
mysql_select_db($dbname); //连接数据库
mysql_query("insert into 表名(字段) values($qi)"); //插入数据
mysql_close($con); //关闭连接
?>

如何自动批量往mysql数据库中插入数据

drop procedure if exists s_card;
delimiter $$
create procedure s_card()
begin
    declare j int(11);
    loop1: while j = 1000 do
        insert into `tablename`(xxxxxx) values (xxx);
        set j = j + 1;
    end while loop1;
end$$
delimiter ;

把上面存储过程运行一下,输入 call s_card() 可以循环插入1000条数据。 如果要指定输入条数可以把数字从外界传入。

mysql数据库表的插入修改,求好的方法和代码

建一个示例表给你:

create table t_result (
    name varchar(30),
    offset int
);
insert into t_result
select 'dao[1]\ch[1]', 1 union
select 'dao[1]\ch[2]', 3 union
select 'dao[2]\ch[1]', 5 union
select 'dao[2]\ch[2]', 7 union
select 'dao[3]\ch[1]', 9 union
select 'dao[3]\ch[2]', 11;
-- 最好备份一下要操作的表
select * into bak_result from t_result order by offset;
-- 进入临时表处理
select * into #t_tmp from t_result where charindex('ch[1]', name) > 1;
-- charindex('ch[1]', name),如果name中包含'ch[1]'子串,则返回所在的位置。否则返回0
-- 处理字符串
update #t_tmp set name = replace(name, 'ch[1]', 'ch[1.5]');
-- replace(name,'ch[1]','ch[1.5]') 把name中'ch[1]'部分替换成'ch[1.5]'
-- 处理offset和插入
declare @currOff int;
while(exists(select top 1 1 from #t_tmp)) -- 只要还有记录
begin
    set @currOff = (select top 1 offset from #t_tmp order by offset asc);
    -- 保存当前处理的记录的标识,这里采用offset
    update t_result set offset = offset + 2 where offset > @currOff;
    -- 更新原表的大于此条要插入记录的offset,+2
    update #t_tmp set offset = offset + 2 where offset > @currOff;
    -- 临时表的也要更新
    insert into t_result select (select name from #t_tmp where offset = @currOff), @currOff + 2;
    -- 插入进去原表
    delete #t_tmp where offset = @currOff;
    -- 删除掉当前记录
end
drop table #t_tmp;
select * from t_result order by offset;

Mysql数据库中,设置id为自动增加,向数据库中插入数据时,SQL语句怎么写?

在建立表的时候设置id为自动增长的:

[id] [int] IDENTITY (1, 1)

SQL语句是:

insert into user(name, passwd) values (name, passwd);

新增一条数据,id就会自动加1。 INSERT INTO 是 SQL 数据库中的语句,可以用于向表格中插入新的行。

扩展资料

(1) 数据记录筛选:

select * from 数据表 where 字段名 = 字段值 order by 字段名 [desc]
-- 按某个字段值降序排列。默认升序 ASC
select * from 数据表 where 字段名 like '%字段值%' order by 字段名 [desc]
select top 10 * from 数据表 where 字段名 = 字段值 order by 字段名 [desc]
select top 10 * from 数据表 order by 字段名 [desc]
select * from 数据表 where 字段名 in ('值1', '值2', '值3')
select * from 数据表 where 字段名 between 值1 and 值2

(2) 更新数据记录:

update 数据表 set 字段名 = 字段值 where 条件表达式
update 数据表 set 字段1 = 值1, 字段2 = 值2 ... 字段n = 值n where 条件表达式

(3) 删除数据记录:

delete from 数据表 where 条件表达式
delete from 数据表
-- 将数据表所有记录删除

(4) 添加数据记录:

insert into 数据表 (字段1, 字段2, 字段3 ...) values (值1, 值2, 值3 ...)
insert into 目标数据表 select * from 源数据表
-- 把源数据表的记录添加到目标数据表

(5) 数据记录统计函数:

  • AVG(字段名):得出一个表格栏平均值
  • COUNT(*;字段名):对数据行数的统计或对某一栏有值的数据行数统计
  • MAX(字段名):取得一个表格栏最大的值
  • MIN(字段名):取得一个表格栏最小的值
  • SUM(字段名):把数据栏的值相加
    引用以上函数的方法:
select sum(字段名) as 别名 from 数据表 where 条件表达式

rs("别名") 获取统计的值,其它函数运用同上。 查询去除重复值:

select distinct * from table1

(6) 数据表的建立和删除:

create table 数据表名称 (字段1 类型1(长度), 字段2 类型2(长度) ...)

(7) 单列求和:

select sum(字段名) from 数据表

参考资料——搜狗百科SQL