Mysql
 sql >> डेटाबेस >  >> RDS >> Mysql

समूह के आधार पर MySQL ऑटो-इन्क्रीमेंट

आप इस तरह की क्वेरी का उपयोग कर सकते हैं:

INSERT INTO autoinc (cid,info,customer)
SELECT
    COALESCE(max(cid),0) +1
    , 'A Customer 1'
    , 12345
FROM autoinc
WHERE customer = 12345;

नमूना

mysql> SELECT * from autoinc;
Empty set (0,00 sec)

mysql> INSERT INTO autoinc (cid,info,customer)
    -> SELECT
    ->     COALESCE(max(cid),0) +1
    ->     , 'A Customer 1'
    ->     , 12345
    -> FROM autoinc
    -> WHERE customer = 12345;
Query OK, 1 row affected (0,00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> SELECT * from autoinc;
+----+------+--------------+----------+
| id | cid  | info         | customer |
+----+------+--------------+----------+
|  1 |    1 | A Customer 1 |    12345 |
+----+------+--------------+----------+
1 row in set (0,00 sec)

mysql> INSERT INTO autoinc (cid,info,customer)
    -> SELECT
    ->     COALESCE(max(cid),0) +1
    ->     , 'A Customer 1'
    ->     , 12345
    -> FROM autoinc
    -> WHERE customer = 12345;
Query OK, 1 row affected (0,00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> SELECT * from autoinc;
+----+------+--------------+----------+
| id | cid  | info         | customer |
+----+------+--------------+----------+
|  1 |    1 | A Customer 1 |    12345 |
|  2 |    2 | A Customer 1 |    12345 |
+----+------+--------------+----------+
2 rows in set (0,00 sec)

mysql> INSERT INTO autoinc (cid,info,customer)
    -> SELECT
    ->     COALESCE(max(cid),0) +1
    ->     , 'B Customer 2'
    ->     , 9876
    -> FROM autoinc
    -> WHERE customer = 9876;
Query OK, 1 row affected (0,00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> SELECT * from autoinc;
+----+------+--------------+----------+
| id | cid  | info         | customer |
+----+------+--------------+----------+
|  1 |    1 | A Customer 1 |    12345 |
|  2 |    2 | A Customer 1 |    12345 |
|  3 |    1 | B Customer 2 |     9876 |
+----+------+--------------+----------+
3 rows in set (0,00 sec)

mysql> INSERT INTO autoinc (cid,info,customer)
    -> SELECT
    ->     COALESCE(max(cid),0) +1
    ->     , 'A Customer 1'
    ->     , 12345
    -> FROM autoinc
    -> WHERE customer = 12345;
Query OK, 1 row affected (0,00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> SELECT * from autoinc;
+----+------+--------------+----------+
| id | cid  | info         | customer |
+----+------+--------------+----------+
|  1 |    1 | A Customer 1 |    12345 |
|  2 |    2 | A Customer 1 |    12345 |
|  3 |    1 | B Customer 2 |     9876 |
|  4 |    3 | A Customer 1 |    12345 |
+----+------+--------------+----------+
4 rows in set (0,00 sec)

mysql>


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. MySQLdb का उपयोग करके आप पायथन से SQL अलगाव स्तर को कैसे बदलते हैं?

  2. PHP जोड़ता है एसक्यूएल इंजेक्शन अभी भी मान्य है?

  3. MySQL हमेशा यहां इंडेक्स मर्ज का उपयोग क्यों नहीं करता है?

  4. mysql जहां सरणी स्ट्रिंग/उपयोगकर्ता नाम में

  5. अद्वितीय बाधा, केवल तभी जब किसी फ़ील्ड में एक विशिष्ट मान होता है