简体   繁体   English

在MySQL中比较数据

[英]Comparing Data in mySQL

I have searched but cannot find an answer that suits my needs specifically. 我已经搜索过,但找不到适合我需要的答案。 I have two sets of data and need to compare a piece of the first (table 1 Description field) with a list (table two) and return the VIP codes for each interface/order. 我有两组数据,需要将第一组数据(表1 Description字段)与列表(表2)进行比较,并返回每个接口/订单的VIP代码。

The only identifier that is the same for any of the descriptions is the 9 digit order ID that ends in '003'. 与任何描述相同的唯一标识符是以'003'结尾的9位数字订单ID。 I need to compare this 9 digit string to the other table that will always start with the order ID but may contain other characters or numbers afterwards. 我需要将此9位数字的字符串与另一个始终以订单ID开头但可能随后包含其他字符或数字的表进行比较。 I know a LIKE comparison will work for the second table but I cannot figure out how to strip the order numbers out of the description field. 我知道类似的比较将适用于第二张表,但是我无法弄清楚如何从描述字段中删除订单号。

UPDATE: Table 1 is a temporary table comparing the output of a router interface command. 更新:表1是一个临时表,用于比较路由器接口命令的输出。 Table 2 is my static account database that has tens of thousands of entries that I do not want to compare to table one. 表2是我的静态帐户数据库,其中包含成千上万个我不想与表1进行比较的条目。 This is why I do not just take table two and compare the order numbers to table one. 这就是为什么我不只是拿表二并将订单号与表一进行比较。 I am specifically asking for help with a way to extract the 9 digit order ID from the description field of Table 1. 我特地寻求帮助,以一种从表1的描述字段中提取9位订单ID的方法。

Table 1 表格1

Interface    Description
Ge 1/0/1     blah_bla_123456003_blahlahlah
Ge 1/0/2     blah_blah_bla_234567003_blahahblh
Ge 1/0/3     b_bla_345678003_blhahblah
Ge 1/0/4     bh_blh_ba_456789003_lahlahbl

Table 2 表2

Order ID       VIP Code
123456003.0    Premier
234567003      Wholesale
345678003.6    Normal
456789003.23   Premier

Expected Results 预期成绩

Order*        VIP Code
123456003     Premier
234567003     Wholesale
345678003     Normal
456789003     Premier

*(stripped from Description)

If you want to take the first 9 digits of ID from Table 2, you could use left(table2.id,9) . 如果要获取表2中ID的前9位,可以使用left(table2.id,9) It will return the 9 first (left) characters from that field. 它将从该字段返回前9个(左)字符。

Then you can use that with a LIKE (using the " % " wildcard) or using regular expressions. 然后,您可以将其与LIKE(使用“ ”通配符)或正则表达式一起使用。

Why not store the data you need in a dedicated column in each table? 为什么不将所需数据存储在每个表的专用列中? Make it a number column and index it, then you can do a very efficient JOIN using that column. 将其设为数字​​列并为其建立索引,然后可以使用该列进行非常有效的JOIN

SELECT * FROM Table_1 LEFT JOIN Table_2 USING(common_index)

you can you something like this : 你可以这样的事情:

select TRUNCATE(tbl2.orderId,0) orderNum, tbl2.vipcode, tbl1.interface
from table2 tbl2 , table1 tbl1
WHERE tbl1.description like CONCAT('%',TRUNCATE(tbl2.orderId,0),'%');

i made a fiddle here 我在这里摆弄

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM