1

I have a table, For example: http://rextester.com/MVG66914

User fills in the fields: name_1(fill any text) and name_2(fill 1 or nothing). If the cell name_1 contains the same text - name_2cell should be increased by 1.

To look like this: http://rextester.com/FSI28573

I can't set UNIQUE or PRIMARY. You can see it on the table examples.

But I can set INDEX Maybe like this:

ALTER TABLE `table15` ADD INDEX(`name_1`);

ALTER TABLE `table15` ADD INDEX(`name_2`);

For example:

INSERT INTO table14a (id, name_1, name_2) VALUES (2, name_1, name_2) ON DUPLICATE KEY UPDATE name_2=name_2+1;

http://rextester.com/YJZ25085

But it should need to be increased by 1 automatically, every time, when user creates a new row with identical text in name_1

INSERT INTO productsfp (?) VALUES (?) ON DUPLICATE KEY UPDATE name_2=name_2+1;

So I tried a variations with DUPLICATE KEY But it didn't work.

I asked question: MySql how to use UPDATE with HAVING? But I realized that I was mistaken in forming request. Cuz all values increases by 1, instead of a serial. Then I read MySql documentation about DUPLICATE KEY and are now stuck in a given issue. Does anyone know how to solve this query?

Community
  • 1
  • 1
Agent Smith
  • 97
  • 1
  • 8

1 Answers1

1

If you specify a composite key involving name_1 and id, then id will auto increment within each name group. The following table definition would achieve this:

CREATE TABLE `table15` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `name_1` VARCHAR(70) NOT NULL, 
    PRIMARY KEY(`name_1`, `id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Note that this won't work with InnoDB, but it will work with MyISAM. Also, I removed the name_2 column, because now the id column will track what you intended for name_2.

Further reading: auto_increment by group

Update:

If you still want a continuous, linear, auto increment ID column, then you can compute it while selecting using the following scheme:

CREATE TABLE `table15` (
    `name_2` INT(11) NOT NULL AUTO_INCREMENT,
    `name_id` INT(11) NOT NULL,
    `name_1` VARCHAR(70) NOT NULL,
    PRIMARY KEY(`name_id`, `name_2`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

SELECT t1.name_2,
       t1.name_id,
       t1.name_1,
       (SELECT COUNT(*) FROM table15 t2
        WHERE t2.name_id < t1.name_id) + t1.name_2 AS id
FROM table15 t1

This assumes that your table would contain a name_id column, which contains integer identifiers for each unique name which appears.

Demo here:

Rextester

Community
  • 1
  • 1
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Yeah, this work: http://rextester.com/JPNIBC73499 But what can I do, if I need `id` cell. I tried several variations, but they did'nt work, I need to consider more deeply. – Agent Smith Feb 16 '17 at 06:54
  • You could compute the `id` value while querying, if you use a numerical `name_id` instead of the text name as the prefix to the primary key. – Tim Biegeleisen Feb 16 '17 at 07:00
  • Very close to what I want to do. I experimented, but it wasn't possible to do such as: `1,2,3,4,5,6 -> Dan, Mike, Mike, Mike, Peter, Peter - 1,1,2,3,1,2`. It didn't get correctly to associate `id` with `name_id` – Agent Smith Feb 16 '17 at 07:42
  • `but it wasn't possible` ... well what actually happened? Your comment doesn't tell us this. – Tim Biegeleisen Feb 16 '17 at 07:44
  • `name_id` is AI and and will introduce the calculation based on the same fields in `name_1`. Ok, it's perfect, I understand how this works. But for example, I have a drop-down list in which I want to select any line, and I don't have a unique field, the type of standard `id ai`, that will count 1,2,3,...8,9,10... By your example I have is greater than equate field doesn't go. http://imgur.com/a/LDPx7 – Agent Smith Feb 16 '17 at 08:15
  • Yes, I think the equation will still work, because we always add on the `name_2` incremented value, which can never be smaller than 1. So, if only one group in the drop down, then you would get `1,2,3,...`. – Tim Biegeleisen Feb 16 '17 at 08:18
  • So that's I mean about I get `1,2,3,4...` or `1,1,2,3...` And all I want to do to make `1,2,3,4...` and `1,1,2,3...` `name_id` returns 0 in every row. And `name_2` or `1,2,3,4...` or `1,1,2,3...` – Agent Smith Feb 16 '17 at 08:32
  • NO... _you_ assign the `name_id` in your original data set. The point is that you give a unique integer id to every name, that's all. – Tim Biegeleisen Feb 16 '17 at 08:34
  • Oh... Something didn't understand, I'll do an hour break and reconsider all of what you and I were talking and tell – Agent Smith Feb 16 '17 at 08:45
  • I tried a variety of options with `name_id`, doesn't want to work together to make it as here: http://rextester.com/FSI28573 – Agent Smith Feb 16 '17 at 13:11