简体   繁体   中英

How to reuse a table in MySQL Workbench?

I read those solutions to the similar questions, but it did not seem to work in my case, I tried:

WITH tmp AS
( (things below is just an **example** to show that it will return a table)
SELECT 
*clounmname* FROM *tablename*
WHERE *XXXX*
)
SELECT *columnname* FROM tmp;

But my mysql workbench says the WITH at the very beginning is misused.. I also tried to add a ; before WITH, but it still does not work..

So I tried:

SELECT *columnname* FROM 
( (things below is just an **example** to show that it will return a table)
SELECT 
*clounmname* FROM *tablename*
WHERE *XXXX*
);

It runs with no error, but returns nothing.. So I don't know what to do, is there something wrong with mysql workbench?

(Version number of my Mysql worbench is 6.2 for Ubuntu 14.04) (The query above is just an example, I know AND and other stuffs.)

I even tried:

CREATE TABLE temp
SELECT *columnname* FROM 
( (things below is just an **example** to show that it will return a table)
SELECT 
*clounmname* FROM *tablename*
WHERE *XXXX*
);
SELECT *columnname* FROM temp;

But nothing returns without any error

MySQL does not support the WITH keyword at this time. Common Table Expressions are not available in MySQL yet.

The solution is to use a temporary or derived table or an inline view.

The query you are using is known as Common table Expression (CTE) . As of now, there is no support for CTE exist in MySQL . Alternative solution is to use either Temporary table or inline view .

So your posted CTE query can be modified with inline view as below. Where the result set returned from inner query will be treated as a temporary result set/view.

select * from
( 
SELECT *clounmname* 
FROM *tablename*
WHERE *XXXX*
) tab

If the result of the inner query will be used more than once then consider pulling that in a temporary table and then use that temporary table like below

create temporary table mytab (*clounmname* datatype);

insert into mytab
SELECT *clounmname* 
FROM *tablename*
WHERE *XXXX*;

select * from mytab;

Make sure that the datatype of the column in temporary table and datatype of the column in select list are same.

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