Copy table with conditions : Copy Table « Table Index « SQL / MySQL






Copy table with conditions

  
  
Drop table CDs;
Drop table CDs2;

CREATE TABLE CDs (
   CDID SMALLINT NOT NULL PRIMARY KEY,
   CDName VARCHAR(50) NOT NULL,
   InStock SMALLINT UNSIGNED NOT NULL,
   Category VARCHAR(20)
);


INSERT INTO CDs
VALUES (101, 'Blood', 10, 'Rock'),
       (102, 'Jazz', 17, 'Jazz'),
       (103, 'Class', 9, 'Classical'),
       (104, 'Violin', 24, NULL),
       (105, 'Blues', 2, 'Blues'),
       (106, 'Tires', 12, 'Country'),
       (107, 'Essence', 5, 'New Age'),
       (108, 'Magic', 42, 'Classical'),
       (109, 'Name', 20, 'Opera'),
       (110, 'Fire', 23, 'Country'),
       (111, 'Live', 18, 'Jazz'),
       (112, 'Blues', 22, 'Blues'),
       (113, 'Stages', 42, 'Blues');



CREATE TABLE CDs2 (
   CDID SMALLINT NOT NULL PRIMARY KEY,
   CDName VARCHAR(50) NOT NULL,
   InStock SMALLINT UNSIGNED NOT NULL
)
SELECT CDID, CDName, InStock
FROM CDs
WHERE Category='Blues' OR Category='Jazz';

select * from CDs2;


           
         
    
  








Related examples in the same category

1.Copy Table Demo
2.Copy Table with Condition
3.Copying a Table
4.Use NULL in where clause
5.Only copy records
6.Using the INSERT Statement to Copy Data
7.Using the REPLACE Statement to Copy Data
8.Copy a table
9.Only copy certain columns
10.Copy one row of data
11.Copy data to temporary table
12.Creating New Tables with SELECT Results
13.Copying Only Selected Data from a Table
14.Copy table with calculation
15.Copying Data into a New Table
16.MySQL truncates the data during copying