我有两个表,假设是AllComputers和MfgComputers。所有计算机中有趣的列是Brand_Name、Model_Name和Model_Number。Mfgcomputers中有趣的专栏是Product_Name。
我可以使用以下查询来显示Brand_Name为HP且Model_Name或Model_Number与Product_Name完全匹配的所有行。
select * from AllComputers,MfgComputers where AllComputers.Brand_Name='HP' AND AllComputers.Model_Number=MfgComputers.Product_Name OR AllComputers.Model_Name=MfgComputers.Product_Name;我想匹配其中的Brand_Name是HP,但使用like或REGEX来匹配所有行,只有当两个匹配中都包含至少三个连续数字时,Model_Number和Model_Name才匹配Product_Name。
因此,如果我有Model_Name作为HP Pavilion500一体机,Product_Name作为HP500一体机,我想只返回两者都有500的匹配项,而不是所有包含" all -in-One PC“的匹配项。
我尝试使用LIKE去掉上面查询中的最后两个=符号,但仍然只匹配完全匹配的符号,因为我不知道如何在整个列中使用通配符。
我知道如果只是匹配一个特定的模型,我可以使用通配符,例如: select * from AllComputers where Model_Number LIKE '%500%';或者select * from AllComputers where Model_Number REGEXP '500';
然而,我想知道是否有可能在至少有3个连续数字的所有模型匹配的列之间这样做。
发布于 2014-01-28 02:09:52
MySQL中的REGEXP '.*[0-9][0-9][0-9].*'包含用于表示数字的通配符,因此可以尝试使用REGEXP
.*等同于LIKE '%',所以实际上就是在两个%s之间夹了3个数字。
发布于 2014-01-28 02:25:05
这将带回至少具有三个连续数字的模型。
where Model_Number REGEXP '[0-9]{3}'根据您的评论进行了更新:
select * from
AllComputers, MfgComputers
where
AllComputers.Brand_Name='HP' AND
AllComputers.Model_Number REGEXP '[0-9]{3}' -- missing AND/OR here
MfgComputers.Product_Name OR -- make sure you are grouping using () for this OR
AllComputers.Model_Name REGEXP '[0-9]{3}' -- missing AND/OR here
MfgComputers.Product_Name;https://stackoverflow.com/questions/21388229
复制相似问题