Concat string as `"@gmail" to Usernames and ignore some lines

时间:2015-05-24 21:46:52

标签: mysql concat

I have a table Table1 with following fields Username NumberID CodeUser Company

Username NumberID CodeUser Company user1 12453 245 G user2 25145T Y user3 1845Z /500 G user4 65421 3452 Y user5 13254 /076 Y

I would have a query result which will show lines user1 and user4 Meaning to ignore WHERE CodeUser is empty or has character starting with / BUT also Username should have the full address if Company=G (that means user1 should show user1@gmail.com)

When I do the query separetely, I have the result, but when I try to combine in ONE query, I have errors.

1. SELECT Username, CONCAT( Username, "@gmail.com" ) ,NumberID, CodeUser, Company
FROM `Table1` 
WHERE Company = 'G';


2. SELECT Username,NumberID,CodeUser,Company
FROM Table1
WHERE CodeUser >'' AND CodeUser NOT LIKE '/%'

Can you help me combine this in ONE query ?

2 个答案:

答案 0 :(得分:0)

Thought you already had all the answers

SELECT Username,
  CASE WHEN Company='G' THEN 
      CONCAT(Username, '@gmail.com') ELSE '' END AS GMail,
  NumberID, CodeUser, Company 
FROM Table1 WHERE CodeUser >'' AND CodeUser NOT LIKE '/%'

答案 1 :(得分:0)

这是你想要的吗?

SELECT (case when company = 'G' then CONCAT(Username, '@gmail.com'
             else UserName
        end) as username,
       NumberID, CodeUser, Company
FROM Table1
WHERE (Company = 'G') or
      (CodeUser > '' AND CodeUser NOT LIKE '/%');