<bdo id='wpEy8'></bdo><ul id='wpEy8'></ul>
      <tfoot id='wpEy8'></tfoot>
    1. <i id='wpEy8'><tr id='wpEy8'><dt id='wpEy8'><q id='wpEy8'><span id='wpEy8'><b id='wpEy8'><form id='wpEy8'><ins id='wpEy8'></ins><ul id='wpEy8'></ul><sub id='wpEy8'></sub></form><legend id='wpEy8'></legend><bdo id='wpEy8'><pre id='wpEy8'><center id='wpEy8'></center></pre></bdo></b><th id='wpEy8'></th></span></q></dt></tr></i><div id='wpEy8'><tfoot id='wpEy8'></tfoot><dl id='wpEy8'><fieldset id='wpEy8'></fieldset></dl></div>
    2. <legend id='wpEy8'><style id='wpEy8'><dir id='wpEy8'><q id='wpEy8'></q></dir></style></legend>

      <small id='wpEy8'></small><noframes id='wpEy8'>

    3. MySQL基于外键的第二个自增字段

      MySQL second auto increment field based on foreign key(MySQL基于外键的第二个自增字段)
      • <bdo id='04eEE'></bdo><ul id='04eEE'></ul>
      • <small id='04eEE'></small><noframes id='04eEE'>

      • <legend id='04eEE'><style id='04eEE'><dir id='04eEE'><q id='04eEE'></q></dir></style></legend>
            <tbody id='04eEE'></tbody>
          <i id='04eEE'><tr id='04eEE'><dt id='04eEE'><q id='04eEE'><span id='04eEE'><b id='04eEE'><form id='04eEE'><ins id='04eEE'></ins><ul id='04eEE'></ul><sub id='04eEE'></sub></form><legend id='04eEE'></legend><bdo id='04eEE'><pre id='04eEE'><center id='04eEE'></center></pre></bdo></b><th id='04eEE'></th></span></q></dt></tr></i><div id='04eEE'><tfoot id='04eEE'></tfoot><dl id='04eEE'><fieldset id='04eEE'></fieldset></dl></div>

              1. <tfoot id='04eEE'></tfoot>

                本文介绍了MySQL基于外键的第二个自增字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我多次遇到这个问题,但还没有找到解决这个问题的MySQL 方法"——我有一个包含用户报告的数据库强>.每个报告都有一个id,我将其作为报告编号显示给我的用户.

                I've come across this problem numerous times but haven't found a "MySQL way" to solve the issue as such - I have a database that contains users and reports. Each report has an id which I display as a report number to my users.

                主要的抱怨是用户对为什么报告从他们的系统中丢失感到困惑.实际情况并非如此.实际上,他们意识到了他们的 ID 之间的差距,并假设这些是缺失的报告,而实际上,这只是因为另一个用户填补了这个自动递增的差距.

                The main complaint is that users are confused as to why reports have gone missing from their system. This is not actually the case. It is actually that they are recognizing a gap between their IDs and assume that these are missing reports, when in actual fact, it is simply becasue another user has filled in this auto-incrementing gap.

                我需要知道是否有办法在 MySQL 中执行此操作:

                I need to know if there is a way to do this in MySQL:

                我是否可以有一个名为 report_numbersecond 自动递增字段,它基于 user_id 字段,该字段具有不同的每个用户的一组自动增量?

                Is it possible that I can have a second auto-increment field called report_number which is based on a user_id field which has a different set of auto-increments per user?

                例如

                |------|---------|---------------|
                |  id  | user_id | report_number |
                |------|---------|---------------|
                |  1   |    1    |       1       |
                |  2   |    1    |       2       |
                |  3   |    1    |       3       |
                |  4   |    2    |       1       |
                |  5   |    1    |       4       |
                |  6   |    1    |       5       |
                |  7   |    2    |       2       |
                |  8   |    3    |       1       |
                |  9   |    3    |       2       |
                |------|---------|---------------|
                

                我为此使用 InnoDB,因为它的外键权重很大.当我添加第二个自动增量字段时,它似乎会抱怨,但我不确定是否有不同的方法来做到这一点?

                I am using InnoDB for this as it is quite heavily weighted with foreign-keys. It appears to complain when I add a second auto increment field, but I wasn't sure if there was a different way to do this?

                推荐答案

                MyISAM 支持第二列自动递增,但 InnoDB 不支持强>.

                MyISAM supports the second column with auto increment, but InnoDB doesn't.

                对于 InnoDB,您可以创建一个触发器 BEFORE INSERT 以获取 reportid 的最大值并将其加一.

                For InnoDB you might create a trigger BEFORE INSERT to get the max value of the reportid and add one to the value.

                DELIMITER $$
                CREATE TRIGGER report_trigger
                BEFORE INSERT ON reports
                FOR EACH ROW BEGIN
                    SET NEW.`report_id` = (SELECT MAX(report_id) + 1 FROM reports WHERE user_id = NEW.user_id);
                END $$
                DELIMITER ;
                

                如果你可以使用 MyISAM 代替,在 MySQL 页面的文档中有一个例子:

                If you can use MyISAM instead, in the documentation of MySQL page there is an example:

                http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html

                CREATE TABLE animals (
                    grp ENUM('fish','mammal','bird') NOT NULL,
                    id MEDIUMINT NOT NULL AUTO_INCREMENT,
                    name CHAR(30) NOT NULL,
                    PRIMARY KEY (grp,id)
                ) ENGINE=MyISAM;
                
                INSERT INTO animals (grp,name) VALUES
                    ('mammal','dog'),('mammal','cat'),
                    ('bird','penguin'),('fish','lax'),('mammal','whale'),
                    ('bird','ostrich');
                
                SELECT * FROM animals ORDER BY grp,id;
                

                哪个返回:

                +--------+----+---------+
                | grp    | id | name    |
                +--------+----+---------+
                | fish   |  1 | lax     |
                | mammal |  1 | dog     |
                | mammal |  2 | cat     |
                | mammal |  3 | whale   |
                | bird   |  1 | penguin |
                | bird   |  2 | ostrich |
                +--------+----+---------+
                

                这篇关于MySQL基于外键的第二个自增字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                SQL query to group by day(按天分组的 SQL 查询)
                What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
                MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                Include missing months in Group By query(在 Group By 查询中包含缺失的月份)
                Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)

                <small id='hVpO1'></small><noframes id='hVpO1'>

                      <tbody id='hVpO1'></tbody>
                  1. <i id='hVpO1'><tr id='hVpO1'><dt id='hVpO1'><q id='hVpO1'><span id='hVpO1'><b id='hVpO1'><form id='hVpO1'><ins id='hVpO1'></ins><ul id='hVpO1'></ul><sub id='hVpO1'></sub></form><legend id='hVpO1'></legend><bdo id='hVpO1'><pre id='hVpO1'><center id='hVpO1'></center></pre></bdo></b><th id='hVpO1'></th></span></q></dt></tr></i><div id='hVpO1'><tfoot id='hVpO1'></tfoot><dl id='hVpO1'><fieldset id='hVpO1'></fieldset></dl></div>

                      <legend id='hVpO1'><style id='hVpO1'><dir id='hVpO1'><q id='hVpO1'></q></dir></style></legend>

                      <tfoot id='hVpO1'></tfoot>
                        • <bdo id='hVpO1'></bdo><ul id='hVpO1'></ul>