2014-01-27 30 views
0

我有兩個表,比方說叫AllComputers和MfgComputers。 Allcomputers中有趣的欄目是Brand_Name,Model_Name和Model_Number。 Mfgcomputers中有趣的欄目是Product_Name。與3個連續號碼匹配的MySql列

我可以使用以下查詢來顯示品牌名稱爲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是惠普但使用LIKE或正則表達式匹配的所有行只有當至少有三個連續的號碼都包含在這兩個比賽中MODEL_NUMBER和MODEL_NAME比賽PRODUCT_NAME。

因此,如果我將HP Pavilion 500 All-in-One個人電腦和Product_Name的型號名稱設爲HP 500多功能一體電腦,我只想返回兩個都是500的配對,而不是所有包含「 All-in-One PC

我試着在上面的查詢中用LIKE標出了最後兩個=標誌,但仍然只匹配完全匹配,因爲我不知道如何爲整個列放置一個通配符。

我知道我可以使用通配符,如果只是針對一個特定的模式匹配,如: SELECT * FROM AllComputers其中MODEL_NUMBER LIKE '%500%'; 或 選擇*來自AllComputers,其中Model_Number REGEXP'500';

但是,我想知道是否有可能在所有具有至少3個連續數字的模型匹配的列之間執行此操作。

回答

0

REGEXP在MySQL中有NUMERICS通配符,所以儘量REGEXP '.*[0-9][0-9][0-9].*'

。*是LIKE '%' 等價,所以這是夾在兩個%s的3 NUMERICS位,有效。

0

這將帶回至少有三個連續數字的模型。

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; 
+0

我已經使用查詢REGEXP在一個表上,但我怎麼使用這兩個表之間的匹配。我試過這個,但得到了一個語法錯誤。選擇* AllComputers,MfgComputers其中AllComputers.Brand_Name ='HP'AND AllComputers.Model_Number REGEXP'[0-9] {3}'MfgComputers.Product_Name或AllComputers.Model_Name REGEXP'[0-9] {3}'MfgComputers.Product_Name ; – Christopher

+0

答案已更新。 –

+0

你在哪裏提到我缺少和/或是我以前有過的=。我只是不確定如何保持等於匹配列,但包含正則表達式以及匹配發生之前完成的正則表達式。 – Christopher