函数(SELECT x,y ...)不起作用

时间:2011-08-05 05:42:46

标签: mysql sql

使用MySQL,我试图使查询更简洁。而不是

UPDATE tbl SET col = Point(
    If(
        condition,
        long CASE WHEN THEN ELSE END,
        a different long CASE WHEN THEN ELSE END
    ),
    If(
        condition,
        long CASE WHEN THEN ELSE END,
        a different long CASE WHEN THEN ELSE END
    )
)

我尝试了以下但未通过

UPDATE tbl SET col = Point(
    SELECT If(condition, x, different(x)), If(condition, y, different(y)) 
    FROM (
        SELECT long CASE WHEN THEN ELSE END x, long CASE WHEN THEN ELSE END y 
        FROM tbl
    ) foo
)

是否无法使用SELECT语句替换函数的参数?或者,我只是做些傻事?或者,还有另一种方法可以使long CASE WHEN THEN ELSE END只出现一次吗?

2 个答案:

答案 0 :(得分:0)

使用类似:

UPDATE tbl AS m_tbl
SET col = (
    SELECT Point( IF(.., x1, x2), 
        IF (.., y1, y2)
    )
    FROM (
        SELECT .. AS x1, 
            .. AS x2, 
            .. AS y1, 
            .. AS y2
        FROM tbl
        WHERE m_tbl.pk_col = tbl.pk_col
    )
)

答案 1 :(得分:0)

您可以尝试按照

的方式构建查询
UPDATE tbl
  INNER JOIN (
    SELECT
      tblKey,
      long CASE WHEN THEN ELSE END x,
      long CASE WHEN THEN ELSE END y 
    FROM tbl
  ) s ON tbl.tblKey = s.tblKey
SET tbl.col = Point(
  IF(condition, s.x, different(s.x)),
  IF(condition, s.y, different(s.y))
)