【问题标题】:Using alias name in another column在另一列中使用别名
【发布时间】:2012-06-22 12:52:56
【问题描述】:

这个问题是我之前问题的扩展部分, Finding number position in string.

我有如下表myTable (myWord ==> varchar(10))

++++++++++++
+ myWord   +    
++++++++++++
+ AB123    +
+ A413     +
+ X5231    +
+ ABE921   +
+ 15231    +
+ 523      +
+ ABC      +
++++++++++++

我想要的如下。

++++++++++++++++++++++++++++++++
+ myWord   + myPos + NewString +
++++++++++++++++++++++++++++++++
+ AB123    + 3     + AB        +
+ A413     + 2     + A         +
+ X5231    + 2     + X         +
+ ABE921   + 4     + ABE       +
+ 15231    + 1     +           +
+ 523      + 1     +           +
+ ABC      + 999   + ABC       +
++++++++++++++++++++++++++++++++

为了得到上面的输出,我使用了下面的查询。

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 myPos,
  if (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)
  )=999,myWord,SUBSTR(myWord,1,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)
  )-1)) as NewString
FROM myTable;

我的问题是

在为列命名为 MyPos 后,为什么我不能在另一列中使用该名称,如下查询所示?

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 myPos,
  if (myPos=999,myWord,SUBSTR(myWord,1,myPos-1)) as NewString
FROM myTable;

但是,这给了我错误

Unknown column 'myPos' in 'field list':

See here for more details

请建议我如何通过使用别名来缩短此查询

【问题讨论】:

    标签: mysql alias


    【解决方案1】:

    尝试将最少的部分作为子选择。你可以别名。

    类似这样的:

    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
    

    SQLFiddle example

    【讨论】:

    • sql fiddle 向我展示了很多行,但没有给我预期的结果
    • 太好了,非常感谢...我也做了一些 RND。见here
    • this question上的任何输入
    【解决方案2】:

    根据@juergend 的回答,我也尝试了以下方法,它也可以正常工作。

    SELECT 
      t1.myWord, t2.l as MyPos, if (t2.l=999,t1.myWord,SUBSTR(t1.myWord,1,t2.l-1)) as NewString
    FROM myTable t1 JOIN  
    (select id, 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 t2
     ON t1.id=t2.id;
    

    注意:为此,我在表中添加了id 以加入两个表。

    Demo

    【讨论】:

      猜你喜欢
      • 2022-12-11
      • 2016-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-09
      • 2021-10-05
      • 1970-01-01
      相关资源
      最近更新 更多