比较两列忽略2个第一个字符

时间:2017-09-28 13:05:56

标签: mysql

分别在列phone_numberphone_no的两个表格中 我必须比较它们但是 在phone_no个数字中带有前缀23 并且在phone_number中他们没有前缀 如何比较它们忽略前缀?

第一张表

    +------------------+-------------+------+-----+-------------------+----------------+
| Field            | Type        | Null | Key | Default           | Extra          |
+------------------+-------------+------+-----+-------------------+----------------+
| id               | int(11)     | NO   | PRI | NULL              | auto_increment |
| name             | text        | NO   |     | NULL              |                |
| password         | text        | NO   |     | NULL              |                |
| email            | text        | NO   |     | NULL              |                |
| phone_no         | text        | NO   |     | NULL              |                |
| checked_by_admin | int(11)     | NO   |     | 1                 |                |
| date             | timestamp   | NO   |     | CURRENT_TIMESTAMP |                |
| download         | int(11)     | NO   |     | 1                 |                |
| sendto           | text        | YES  |     | NULL              |                |
| meet             | timestamp   | YES  |     | NULL              |                |
| note             | longtext    | NO   |     | NULL              |                |
| sec              | int(11)     | NO   |     | 1                 |                |
| alt_phone        | text        | NO   |     | NULL              |                |
| work             | text        | NO   |     | NULL              |                |
| company          | text        | NO   |     | NULL              |                |
| reg_by           | text        | NO   |     | NULL              |                |
| op_status        | text        | NO   |     | NULL              |                |
| lang             | varchar(15) | NO   |     | NULL              |                |
| deposit_by       | varchar(30) | NO   |     | NULL              |                |
| list_name        | text        | NO   |     | NULL              |                |
| web              | int(11)     | YES  |     | NULL              |                |
+------------------+-------------+------+-----+-------------------+----------------+

第二张表

`
+-------------------------+------------------------------------------------------------------+------+-----+-------------------+-----------------------------+
| Field                   | Type                                                             | Null | Key | Default           | Extra                       |
+-------------------------+------------------------------------------------------------------+------+-----+-------------------+-----------------------------+
| lead_id                 | int(9) unsigned                                                  | NO   | PRI | NULL              | auto_increment              |
| entry_date              | datetime                                                         | YES  |     | NULL              |                             |
| modify_date             | timestamp                                                        | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| status                  | varchar(6)                                                       | YES  | MUL | NULL              |                             |
| user                    | varchar(20)                                                      | YES  |     | NULL              |                             |
| vendor_lead_code        | varchar(20)                                                      | YES  |     | NULL              |                             |
| source_id               | varchar(50)                                                      | YES  |     | NULL              |                             |
| list_id                 | bigint(14) unsigned                                              | NO   | MUL | 0                 |                             |
| gmt_offset_now          | decimal(4,2)                                                     | YES  | MUL | 0.00              |                             |
| called_since_last_reset | enum('Y','N','Y1','Y2','Y3','Y4','Y5','Y6','Y7','Y8','Y9','Y10') | YES  | MUL | N                 |                             |
| phone_code              | varchar(10)                                                      | YES  |     | NULL              |                             |
| phone_number            | varchar(18)                                                      | NO   | MUL | NULL              |                             |
| title                   | varchar(4)                                                       | YES  |     | NULL              |                             |
| first_name              | varchar(30)                                                      | YES  |     | NULL              |                             |
| middle_initial          | varchar(1)                                                       | YES  |     | NULL              |                             |
| last_name               | varchar(30)                                                      | YES  |     | NULL              |                             |
| address1                | varchar(100)                                                     | YES  |     | NULL              |                             |
| address2                | varchar(100)                                                     | YES  |     | NULL              |                             |
| address3                | varchar(100)                                                     | YES  |     | NULL              |                             |
| city                    | varchar(50)                                                      | YES  |     | NULL              |                             |
| state                   | varchar(2)                                                       | YES  |     | NULL              |                             |
| province                | varchar(50)                                                      | YES  |     | NULL              |                             |
| postal_code             | varchar(10)                                                      | YES  | MUL | NULL              |                             |
| country_code            | varchar(3)                                                       | YES  |     | NULL              |                             |
| gender                  | enum('M','F','U')                                                | YES  |     | U                 |                             |
| date_of_birth           | date                                                             | YES  |     | NULL              |                             |
| alt_phone               | varchar(12)                                                      | YES  |     | NULL              |                             |
| email                   | varchar(70)                                                      | YES  |     | NULL              |                             |
| security_phrase         | varchar(100)                                                     | YES  |     | NULL              |                             |
| comments                | varchar(255)                                                     | YES  |     | NULL              |                             |
| called_count            | smallint(5) unsigned                                             | YES  |     | 0                 |                             |
| last_local_call_time    | datetime                                                         | YES  | MUL | NULL              |                             |
| rank                    | smallint(5)                                                      | NO   | MUL | 0                 |                             |
| owner                   | varchar(20)                                                      | YES  | MUL |                   |                             |
| entry_list_id           | bigint(14) unsigned                                              | NO   |     | 0                 |                             |
+-------------------------+------------------------------------------------------------------+------+-----+-------------------+-----------------------------+
`

2 个答案:

答案 0 :(得分:0)

您可以使用MID()函数删除前两个字符。 用这种方式进行比较:

where MID(phone_number, 3, 20) = phone_no

答案 1 :(得分:0)

在比较子句中使用SUBSTR函数:

...
WHERE SUBSTR(table1.phone_no, 3) = table2.phone_number