In one table there is a Фамилия , first Имя , Отчество student. Question. How to make a function to display all three columns in the format Фамилия.А.А. (initials).

And then display in a related table.

  • If you are talking about a function, then why are we talking about three columns, the function returns one value and it cannot do three columns in any way. Something like concat(фамилия,'.',subtr(имя,1,1),'.',substr(отчество,1,1),'.') Should return approximately what you apparently want. But what it means to “display in a related table”, I don’t put my mind on it, you can write it down in a table and read it from it, there’s no such thing. And connected by the way by what criterion - Mike
  • @Mike honestly, I myself do not fully understand what is being demanded of me. They told us so. Create a function to form a string with a surname and initials of the form "Last Name IO" based on the surname, name and patronymic. - Artik Slayer

1 answer 1

You can do the following. To execute the query, you need to install a separator // in the client, for example, in the console mysql client, this is done using the DELIMITER // command DELIMITER //

 DROP FUNCTION IF EXISTS fio// CREATE FUNCTION fio( `name` TEXT CHARSET utf8, `surname` TEXT CHARSET utf8, `patronymic` TEXT CHARSET utf8 ) RETURNS TEXT CHARSET utf8 READS SQL DATA BEGIN RETURN CONCAT( `surname`, ' ', SUBSTRING(`name`, 1, 1), '. ', SUBSTRING(`patronymic`, 1, 1), '.'); END// 

You can use the function as follows.

 SELECT fio(name, surname, patronymic) FROM users; 
  • What does EXISTS mean in if? - Artik Slayer
  • Check whether there is a function, if you try to remove a non-existent function - an error will be generated. - cheops
  • Is there a semicolon instead of a double slash? What happens if I do not record the first line? - Artik Slayer
  • You cannot then use a semicolon inside the function, and you will need at least one semicolon in the body of the function. - cheops
  • Is DELIMITER valid only for the current request or for subsequent ones? - Artik Slayer