开发者

How does this SQL query work?

开发者 https://www.devze.com 2023-02-07 18:05 出处:网络
SELECT REPLACE(TRANSLATE(LTRIM(RTRIM(\'!! ATHEN !!\',\'!\'), \'!\'), \'AN\', \'**\'),\'*\',\'TROUBLE\') F开发者_开发百科ROM DUAL;
SELECT REPLACE(TRANSLATE(LTRIM(RTRIM('!! ATHEN !!','!'), '!'), 'AN', '**'),'*','TROUBLE') F开发者_开发百科ROM DUAL;

I'm confused about above question how this query works ?


SELECT 
  REPLACE(
    TRANSLATE(
      LTRIM(
        RTRIM('!! ATHEN !!','!')
        , '!')
      , 'AN'
      , '**')
    ,'*'
    ,'TROUBLE')
FROM DUAL;

It doesn't work on MySQL.

It would return a single constant value. DUAL is a system table with one record. It allows you to select a constant value when you don't actually have a table to query from, because dual is always there. FROM DUAL is optional in MySQL and in Oracle from 23c on.

What this query should do:

  • It takes the text '!! ATHEN !!'
  • It trims the '!' on both ends (ltrim and rtrim)
  • It 'translates' A and N to * and *.
  • It replaces each * with 'TROUBLE'

So I'd expect the output to be something like ' TROUBLETHETROUBLE '.

That is, when you run it on Oracle. MySQL won't accept the second parameter in RTRIM as Nanne pointed out in the comments.

0

精彩评论

暂无评论...
验证码 换一张
取 消