1

Consider the following table:

id  foo
-----------
1   bar
2   baz
3   bat

Desired output (from SQL query):

id str_id foo
--------------
1  str1   bar
2  str2   baz
3  str3   bat

In this case, "str" is concatenated to every id. Is this possible and if so how, in MySQL?

2 Answers 2

3

Use CONCAT() to concatenate strings in MySQL.

SELECT id, CONCAT("str", id) AS str_id, foo FROM yourtable
Sign up to request clarification or add additional context in comments.

2 Comments

LOL...so obvious I'm ashamed actually...thought it would be quite complex, using temp tables and combining them and what not
No worries. MySQL has a lot of functions. Might be worth it to browse the link I provided.
2
SELECT `id`, CONCAT('str', `id`) AS `str_id`, `foo`
FROM `tbl`
ORDER BY `id` ASC;

1 Comment

I'll say it again( see below): LOL...so obvious I'm ashamed actually...thought it would be quite complex, using temp tables and combining them and what not. You were 7 secs faster so here's the accepted-answer karma. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.