English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

i've got this table called items and there's a blank table called expensive items, what code do i write to insert all items in ITEMS which cost more than 50 into EXPENSIVEITEMS table?

2007-02-03 05:51:44 · 3 answers · asked by Scottsman 3 in Computers & Internet Programming & Design

3 answers

insert into EXPENSIVEITEMS
select * from ITEMS where cost > 50;

This of course assumes that ITEMS and EXPENSIVEITEMS have the same structure. But why would you want to keep a table of expensive items when they already exist in items and you can find them with where cost > 50?

2007-02-03 05:55:14 · answer #1 · answered by BigRez 6 · 2 0

I agree with the previous answer, why would you want to create a separate table to store the same data you've already got? It sort of defeats the purpose of having a databse and querying it in the first place because your new table isn't going to be kept automatically up to date when new items are inserted into the ITEMS table. If you really need to do this, you'd be better off writing a View. A View is basically just a query from other tables that can be accessed as if it was it's own table, this means it's dynamic and constantly updated with whatever data is in the table(s) it's based on. More about Views here:

http://www.fluffycat.com/SQL/Views/

To be honest thought it seems unlikely you'd even need that, whenever you need to get a list of these 'expensive items' you'd just use a query to select only those with a certain price from your ITEMS table.

2007-02-03 06:08:44 · answer #2 · answered by Bamba 5 · 2 0

BigRez answered the question as asked, but Bamba has the correct way of doing this.

Data should never be duplicated

2007-02-03 13:57:32 · answer #3 · answered by Jeffrey F 6 · 0 0

fedest.com, questions and answers