Mysql
 sql >> Database >  >> RDS >> Mysql

Utilizzo del nome alias in un'altra colonna

Prova a mettere la parte minima come sottoselezione. Che puoi alias.

Qualcosa del genere:

SELECT 
  mytable.myWord, 
  myPos.l,
  if (l=999,mytable.myWord,SUBSTR(mytable.myWord,1,l-1)) as NewString
FROM myTable, 
(select myword, LEAST (
    if (Locate('0',myWord) >0,Locate('0',myWord),999),
    if (Locate('1',myWord) >0,Locate('1',myWord),999),
    if (Locate('2',myWord) >0,Locate('2',myWord),999),
    if (Locate('3',myWord) >0,Locate('3',myWord),999),
    if (Locate('4',myWord) >0,Locate('4',myWord),999),
    if (Locate('5',myWord) >0,Locate('5',myWord),999),
    if (Locate('6',myWord) >0,Locate('6',myWord),999),
    if (Locate('7',myWord) >0,Locate('7',myWord),999),
    if (Locate('8',myWord) >0,Locate('8',myWord),999),
    if (Locate('9',myWord) >0,Locate('9',myWord),999)
  ) as l from mytable)
as myPos
where myPos.myword = mytable.myword

Esempio SQLFiddle