dongpan9760
2015-09-08 23:13
浏览 26
已采纳

SQL Auto在同一个表中多次递增

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.

The issue:

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:

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"

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.

Thanks in advance, Rich

图片转代码服务由CSDN问答提供 功能建议

我为网站上的内置管理面板提供了一个基本的HTML输入表单,我正在为朋友设计 矿。 他的想法是让他将使用PHP脚本编写的数据发布到数据库中,然后再将其吐回到索引页面。

问题:

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

EG:

他发布“Hello”一次,给出了ep_num为“1”
输入“World” 一次,给它ep_num为“1”
再次输入“Hello”,给出ep_num为“2”

我度过了漫长的一天 工作,并相信 mysql_insert_id()可能是我正在寻找的,我只是努力让我的大脑工作,并希望你们中的一个人帮助我。

提前致谢, Rich

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dqsp60748 2015-09-09 00:08
    已采纳
    insert into t (name, ep_num) select 'b', count(*)+1 from t where name='b'; 
    

    Demo on sqlfiddle

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douzhanrun0497 2015-09-08 23:28
    INSERT INTO table (name,ep_num) VALUES ("hello",1)
    ON DUPLICATE KEY UPDATE ep_num=ep_num+1;
    

    To use this statement you need to make column name a primary key or a Unique index and make id an auto increment value if you are changing name to primary key. For more information on Duplicate key update

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题