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 tha开发者_如何学编程t 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.
精彩评论