2

The problem

I have a SQL database named "matches" with a textfield named "events". In this event there is some data and I want to add a string to this data. For example: the existing data is "Goal: Aguero" and I want to add, with a SQL statement, the data: ", Yellow card: Ozil", so that I get the result: "Goal: Aguero, Yellow: Ozil"

What I've already tried

I've tried many things but I don't get the right solution. I've tried the dot function: 'Goal' . 'Yellow' but it doesn't work. I've also tried the CONCAT() function, but it doesn't work too.

Some code that I've tried:

$sql = "UPDATE matches
SET events.UpdateBy = CONCAT('Goal: Aguero',' ','Yellow card: Ozil')
ORDER BY id DESC LIMIT 1";

$sql = "UPDATE matches
SET events = 'Goal: Aguero' . ' Yellow card: Ozil'
ORDER BY id DESC LIMIT 1";

Can anyone help me? Many thanks in advance!

Community
  • 1
  • 1
T. Hægh
  • 109
  • 1
  • 9

1 Answers1

4

You simply specify the column name as the first argument and use CONCAT_WS which is NULL safe i.e. it'll work even if events column is NULL:

UPDATE matches
SET events = CONCAT_WS(', ', events, 'Yellow card: Ozil')
WHERE ...

Demo on DB<>Fiddle

PS: I would rather suggest creating a table of events e.g. matchevents(id, event_id, event_type_id, player_id, time, etc). It is better that way.

Salman A
  • 262,204
  • 82
  • 430
  • 521