简体   繁体   中英

How to union same table in MySQL?

Now I have the following sql:

SELECT MAX(a), MIN(a)
FROM t1 join t2 join t3
UNION ALL 
SELECT MAX(b), MIN(b)
FROM t1 join t2 join t3
UNION ALL 
SELECT MAX(c), MIN(c)
FROM t1 join t2 join t3
UNION ALL 
SELECT MAX(d), MIN(d)
FROM t1 join t2 join t3

While table t1, t2, t3 are all huge, join them is very slow.

How can I join them just once in this query? Alias the table or create a temp table(query scope)?

And

  1. I don't want to transform them into 1 row 4 column.
  2. I tried to create view. It doesn't work.

Thanks in advance!

In SQL Server, you can use a lateral join:

SELECT v.x
FROM t1 join
     t2
     on . . . join
     t3
     on . . . cross apply
     (values (a), (b), (c), (d)) v(x);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM