mysql获取字符左侧的字符串并将其与另一列进行比较

时间:2013-12-17 21:52:34

标签: mysql string-comparison string-split

我正在运行此SQL查询

SELECT DISTINCT  a.user_login, a.user_email, c.domain
FROM wp_users a
JOIN wp_usermeta b ON a.id = b.user_id
JOIN wp_blogs c ON b.meta_value = c.site_id
WHERE meta_key =  'primary_blog' and
ORDER BY  `a`.`ID` ASC 
LIMIT 0 , 100

结果

user_login        user_email                domain
abc               xys@example.com           laskdlasd.com
cdestefano        cdestefano@example.com    kaskdjkasjdkasd
fran              fran@example.com          kasdllasdlasd

我只想要user_login不等于@之前的字符串的行,即从上面的结果我只需要下面的行

abc               xys@example.com           laskdlasd.com

我尝试了下面的代码,但它不能正常工作

SELECT DISTINCT a.user_login, a.user_email, c.domain
FROM wp_users a
JOIN wp_usermeta b ON a.id = b.user_id
JOIN wp_blogs c ON b.meta_value = c.site_id
WHERE meta_key =  'primary_blog'
AND a.user_login != LEFT( a.user_email, LOCATE(  '@', a.user_email ) ) 
ORDER BY  `a`.`ID` ASC 
LIMIT 0 , 100

如何获得理想的结果?

2 个答案:

答案 0 :(得分:0)

WHERE子句

上添加此内容
AND SUBSTRING(REPLACE(user_email, user_login, ''), 1, 1) <> '@'

答案 1 :(得分:0)

尝试

SELECT *
FROM wp_users a
JOIN wp_usermeta b ON a.id = b.user_id
JOIN wp_blogs c ON b.meta_value = c.site_id
WHERE user_login  != substring_index(user_email,'@',1)