0

As my application is expanding, I now am changing the structure of my database; I now want to control file types within the database. I wanted to start with the current file types already in the database. My Database now has a [simplified] 2 table structure like:

tbFiles: pkFileID, fileType, fileName

tblFileType: pkFileType, typeName, typeDesc

I am trying to have the output of a SELECT query update into the newly created tblFileType table. I have tried among other things:

UPDATE tblFileType
INNER JOIN
(SELECT DISTINCT fileType FROM tblFiles) as X
SET typeName = fileType

but I always seem to get 0 row(s) affected.

When I run

SELECT DISTINCT fileType
FROM  `tblFiles`

I get Showing rows 0 - 22 (~23 total, Query took 0.0074 sec)

I know this must be simple, but why is the UPDATE query not affecting 23 rows?

2 Answers 2

2

You need to add a JOIN condition like ON t1.fileType = x.fileType as follows:

UPDATE tblFileType t1
INNER JOIN
(
    SELECT DISTINCT fileType 
    FROM tblFiles
)as X ON t1.fileType = x.fileType
SET t1.typeName = X.fileType

Update: Since the table tblFileType is blank, you will need to use INSERT something like:

INSERT INTO tblFileType(typeName )
SELECT DISTINCT fileType 
FROM tblFiles
WHERE -- a condition here
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the quick reply. I originally had an ON clause of 1=1, but it didn't do anything. Your query as shown has an ON clause, but the table tblFileType is blank, so there is nothing to match records with the ON. When making small corrections to the Query above (t1.fileType -> t1.typeName, x.fileType -> X.fileType, ) , I still get 0 Records returned.
@SableFoste You can use INSERT INTO ... SELECT see my edit.
thank you very much, that was it! I knew it was obvious; sometimes I just get a 'duh' moment!
1

you just want to populate the table - not update anything in there (especially since nothing exists yet)

INSERT INTO tblFileType(typeName )
SELECT DISTINCT fileType FROM tblFiles

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.