5

Suppose I have a query likes:

SELECT name FROM product WHERE id = 28;

The result is "HTC Desire HD"

I want this result to be replaced with XX in the string "I Like XX", after replacing, the string is "I Like HTC Desire HD"

Likes the PHP printf function, can I use MySQL to do that?

4
  • this is not ideal.use presentation language like php to do the formatting should be better Commented Sep 6, 2011 at 4:47
  • 1
    @ajreal: It seems about the same either way. Similar to querying someone's full name as SELECT CONCAT(firstName, ' ', lastName). Unless that's just bad practice I'm not aware of (I'm no expert on SQL). Commented Sep 6, 2011 at 5:28
  • is not the same.the formatted strings are meant for presentation let the presentation language to do the job Commented Sep 6, 2011 at 5:31
  • @ajreal PHP does not answer the question. You don't know the context. If you need a fully SQL report, you'll need pure SQL formatting. Commented Aug 9, 2023 at 19:01

2 Answers 2

9

That would be:

select 'I Like ' || name from product where id = 28;

in regular SQL. I'm not entirely certain that will work in MySQL unless you have PIPES_AS_CONCAT configured, but the equivalent:

select CONCAT('I Like ', name) from product where id = 28;

should be fine.

Sign up to request clarification or add additional context in comments.

1 Comment

I think in MySQL it's double pipes (||), as long as your database is set to PIPES_AS_CONCAT mode :-) +1 !
4

Like a CONCAT()?

SELECT CONCAT('I Like ',name)
FROM   product
WHERE  id = 28;

Comments

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.