function(SELECT x, y ...) doesn't work
Using MySQL, I am trying to make a query less verbose. Instead of
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
)
)
I tried the following but failed
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
)
Is it not possible to use a SEL开发者_如何学GoECT statement to replace arguments to a function? Or, am I just doing something silly? Or, is there another way to make this long CASE WHEN THEN ELSE END
appear only once?
Use something like:
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
)
)
You could try to build your query along the lines of
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))
)
精彩评论