仅在sql查询中比较表内容的第一个数字

时间:2014-05-26 07:01:17

标签: mysql

我有以下MySQL查询

UPDATE customers_basket
SET flag = '1'
WHERE products_id = '8523'
AND customers_id = '7221'

但是products_id后缀为':'和其他数字,例如它可能是

8523:d32ee9341c0e1d75b8ab25be583a3829.

其他数字与所选属性有关。

MySQL是否能够在处理查询的products_id部分时仅将WHERE与冒号之前的值进行比较?

5 个答案:

答案 0 :(得分:1)

你可以试试

UPDATE customers_basket
SET flag = '1'
WHERE 
substring_index(products_id,':',1) = '8523'
AND customers_id = '7221'

答案 1 :(得分:1)

使用"喜欢"运营商:

UPDATE customers_basket
SET flag = '1'
WHERE products_id LIKE '8523%'  // return all id starting by 8523...
AND customers_id = '7221'

"像"运营商:

LIKE '%a%' // all which contains 'a'
LIKE 'a%'  // all which starting by 'a' 
LIKE '%a'  // all which ending by 'a' 

答案 2 :(得分:0)

试  UPDATE customers_basket SET flag = '1' WHERE products_id like '8523:%' AND customers_id = '7221'

答案 3 :(得分:0)

您可以使用LIKE:

UPDATE customers_basket SET flag = '1' WHERE products_id like '8523:%' AND customers_id = '7221'

LIKE:http://dev.mysql.com/doc/refman/5.0/en/pattern-matching.html

的解释

答案 4 :(得分:0)

在这种情况下你也可以使用like子句:

如 -

UPDATE customers_basket
SET flag = '1'
WHERE products_id LIKE '8523%'
AND customers_id = '7221';

这可能对您有所帮助。