MySQL:如何在表列中修剪前缀关键字?

时间:2011-09-05 16:12:29

标签: mysql

我使用关键字m来表示来自member的帖子,u来自user

所以帖子owner id会有类似的东西, m-1m-2u-1

m-1member id 1

m-2member id 2

u-1user id 1

这是表格帖子,

needle_id   needle_title    needle_content  needle_published    owner_id    pg_id   
    1       a               xxx status      1                   m-1         39      
    2       b               xxx status      1                   m-2         39  
    3       c               xxx status      1                   m-3         39  
    4       d               xxx status      1                   u-1         39  
    5       e               xxx status      1                   m-1         39

和表成员,

mem_id  mem_screenname      mem_email           mem_key     cat_id
1       Member one          a@sample.com        xxx         1
2       Member two          b@sample.com        xxx         1
3       Member three        c@sample.com        xxx         1

这是我的查询,

SELECT
n.needle_id as needleID,
n.needle_title as needleTitle,
n.needle_content as needleContent,
n.needle_published as needleApproved,
n.mem_id as needleOwnerID,
n.needle_created as needleCreated,

m.mem_screenname as needleOwnerScreenname,
m.mem_email as needleOwnerEmail,
m.mem_key as needleOwnerKey,
m.cat_name as needleOwnerType

FROM root_string_needles AS n

    LEFT JOIN 
    (
        SELECT 
        mm.mem_id,
        mm.mem_screenname,
        mm.mem_email,
        mm.mem_key,
        ccmm.cat_name

        FROM root_members_cfm  AS mm

        LEFT JOIN root_category_members AS ccmm
        ON ccmm.cat_id = mm.cat_id
    ) AS m 
    ON m.mem_id = n.owner_id 

WHERE n.pg_id = '39'

AND m.mem_screenname IS NOT NULL
AND n.needle_published != '0'

ORDER BY n.needle_created DESC

我坚持ON m.mem_id = n.owner_id,因为owner id使用了前缀,但它们不匹配。

那么如何修剪owner id列中的前缀,以便列mem id可以匹配owner id

1 个答案:

答案 0 :(得分:1)

@Mikael有正确的答案。要实际修剪数据,您可以使用MySQL mid函数:

MID(owner_id,3,2)

您还可以找到短划线( - ),然后在短划线后提取所有数据。