简体   繁体   中英

Column count doesn't match value count at row 1 when Copying Data from two tables into one table

I recieve an error "Column count doesn't match value count at row 1" when i try to run this script

INSERT INTO COMPANY_AND_SUPPLIERS VALUES (
(SELECT COMPANY_NAME FROM SUPPLIER),
(SELECT PRODUCT_NAME FROM PRODUCT)
);

Im trying to copy all records of two columns from two different tables into another Table.

i did some research and cant find any solutions without using a WHERE clause to add specific values such as where COMPANY_NAME = 'Alice';

EDIT

CREATE TABLE COMPANY_AND_SUPPLIERS (

        COMPANY_NAME VARCHAR (40) NOT NULL DEFAULT 'EMPTY',
        PRODUCT_NAME VARCHAR(40) NOT NULL DEFAULT 'EMPTY' ,
        TOTAL_PRODUCTS VARCHAR(40) NOT NULL DEFAULT 'EMPTY',
        CONSTRAINT SUPPLIER_PKEY PRIMARY KEY(COMPANY_NAME) ,
        CONSTRAINT SUPPLIER_FKEY FOREIGN KEY (COMPANY_NAME) REFERENCES SUPPLIER(COMPANY_NAME)

        );
UPDATE COMPANY_AND_SUPPLIERS
        SET TOTAL_PRODUCTS = (SELECT COUNT(*) AS TOTALPRODUCTS
        FROM PRODUCT);

If you insert data into a table without specifying the column names SQL implies you are aware of the number of columns and that you are using them in the right order. But if you omit one or more columns, you get the aforementioned error.

To enable inserting without the need to specify values for all columns, rewrite the statement similar to

INSERT INTO COMPANY_AND_SUPPLIERS(COMPANY_NAME, PRODUCT_NAME)
VALUES((SELECT COMPANY_NAME FROM SUPPLIER), (SELECT PRODUCT_NAME FROM PRODUCT))

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