0

我有一张像这样的“大”桌子

    id  user type comment
    6   1    A    id '3' - #8
    7   1    A    id '3' - #9
    8   3    B    
    9   3    B  

我想提取哈希后的数字并将其与 id 列连接以具有以下内容(当单引号之间的数字等于用户时)

    id1 id2  user  type
    6   8    3     B
    7   9    3     B 
4

1 回答 1

0

A 创建您的表的副本,其名称为 bigtable 并插入您提供给我们的那些值:

mysql> create table bigtable (id int, user_id int, type varchar(10), comment varchar(30));
Query OK, 0 rows affected (0.04 sec)

mysql> insert into bigtable values (6,1, 'A', 'id 3 - #8'),(7,1,'A', 'id 3 - #9'),(8,3, 'B','' ),(9,3, 'B','');
Query OK, 4 rows affected (0.01 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from bigtable;
+------+---------+------+-----------+
| id   | user_id | type | comment   |
+------+---------+------+-----------+
|    6 |       1 | A    | id 3 - #8 |
|    7 |       1 | A    | id 3 - #9 |
|    8 |       3 | B    |           |
|    9 |       3 | B    |           |
+------+---------+------+-----------+
4 rows in set (0.00 sec)

我做了一个自加入并使用 substrn_index 函数:

mysql> select b1.id,b2.id,b1.user_id,b1.type 
from bigtable as b1 join bigtable as b2 
on b1.id=SUBSTRING_INDEX(b2.COMMENT,'#',-1);
+------+------+---------+------+
| id   | id   | user_id | type |
+------+------+---------+------+
|    8 |    6 |       3 | B    |
|    9 |    7 |       3 | B    |
+------+------+---------+------+
2 rows in set (0.00 sec)

我希望这对你有帮助

于 2012-06-08T18:40:23.873 回答