如果表中至少有一行,则以下查询有效。如果它返回,我希望它返回 1 NULL
。
mysql> desc users_map_item;
+----------------+-----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-----------------------+------+-----+---------+-------+
| map_user_id | mediumint(8) unsigned | NO | MUL | NULL | |
| map_item_id | mediumint(8) unsigned | NO | PRI | NULL | |
| unique_item_id | mediumint(8) unsigned | NO | | NULL | |
| item_owner | mediumint(8) unsigned | NO | | NULL | |
| privilege | tinyint(1) | YES | | NULL | |
+----------------+-----------------------+------+-----+---------+-------+
SELECT case when unique_item_id IS NULL then isnull(max(unique_item_id)+1) else
max(unique_item_id)+1 end as unique_item_id FROM users_map_item WHERE map_user_id = 1;