简体   繁体   中英

How do I make a SQL table from the result of another select?

I'm asking for MySQL specifically, but the answer might work for a lot of SQL databases.

If I have a select statement like this:

select * from users where age > 5;

How do I assign that to a new table?

insert into old_farts
select * from users where age > 5;

Here's the reference page.

At least in Oracle you can do something like

CREATE TABLE SOME_KIDS AS
  SELECT * FROM USERS WHERE AGE > 5;

You need to use the INSERT...SELECT syntax -

INSERT INTO NewTable SELECT * FROM Users WHERE AGE > 5

(Edited to add: In SQL that would be SELECT * INTO NewTable FROM Users WHERE AGE > 5 )

It sounds like what you actually want is a view .

CREATE OR REPLACE VIEW users_over_five AS (SELECT * FROM users WHERE age > 5);

Unless you want a non-normalized database , or are just moving data around for maintenance, a view is the way to go.

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