SQL Auto Increment在同一张表中多次 [英] SQL Auto Increment several times in the same table

查看:112
本文介绍了SQL Auto Increment在同一张表中多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的一个朋友设计的网站上的内置管理面板有一个基本的HTML输入表单.他的想法是让他将用PHP脚本编写的数据发布到数据库中,然后将其放回到索引页面上.

I have a basic HTML input form for an inbuilt admin panel on a website that I'm designing for a friend of mine. The idea is for him to post data scripted with PHP to the database, which is then spat back out onto the index page.

问题:

每个条目显然在db中都有自己的列(id,name,ep_num等). id将自动递增到数据库中,名称被收集为$_POST['name'],但是对于ep_num,我想为每个名称分配自动递增.

Each entry obviously has its own column in the db (id, name, ep_num etc). The id is auto incremented into the database, the name is gathered as a $_POST['name'], however for ep_num, I would like to assign auto increments per name.

EG:

他发布了一次"Hello",它的ep_num为"1"
他输入了世界"一次,给出了ep_num为"1"
他再次输入"Hello",其ep_num为"2"

He posts "Hello" once, it is given the ep_num of "1"
He inputs "World" once, it is given the ep_num of "1"
He inputs "Hello" again, it is given the ep_num of "2"

我工作了很长的一天,并且相信mysql_insert_id()可能就是我想要的,我只是在努力地发挥自己的作用,并希望你们中的一个能帮助我.

I've had a long day at work, and believe that mysql_insert_id() may be what I'm looking for, I'm just struggling to put my brain to work and was hoping one of you guys to aid me.

预先感谢, 丰富

推荐答案

insert into t (name, ep_num) select 'b', count(*)+1 from t where name='b'; 

在sqlfiddle上进行演示

这篇关于SQL Auto Increment在同一张表中多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆