[Solved] Updating one table with values from another table

Creating tables and queries
Post Reply
skysnet
Posts: 5
Joined: Mon Jan 23, 2017 10:21 am

[Solved] Updating one table with values from another table

Post by skysnet »

I have two tables. One contains contact information, and the other contains professional information.

They can connect to each other by a column called UNIQUE_ID

A UNIQUE_ID will have only one row in contact information table but there could be multiple rows for that UNIQUE_ID in professional information.

In the ideal world, I would know how to create an SQL query to pull and filter results as I need it.

But in the absence of that, I created the same fields (Name, email, etc) on the professional information table and I'm trying to bring the personal information (PILOT_BASIC) over to the professional table (PILOT_CERT)

Code: Select all

UPDATE PILOT_CERT
SET 
PILOT_CERT.STREET_1 = PILOT_BASIC.STREET_1
FROM PILOT_BASIC AS PILOT_CERT
INNER JOIN PILOT_BASIC AS PILOT_CERT
WHERE PILOT_CERT.UNIQUE_ID = PILOT_BASIC.UNIQUE_ID
This gives syntax error.

Any help would be greatly appreciated.
Last edited by Hagar Delest on Wed Jan 25, 2017 8:58 am, edited 1 time in total.
Reason: tagged [Solved].
OpenOffice 5.0.4.2 on Windows 10
User avatar
robleyd
Moderator
Posts: 5081
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: Updating one table with values from another table

Post by robleyd »

What is the error message?
Cheers
David
OS - Slackware 15 64 bit
Apache OpenOffice 4.1.15
LibreOffice 24.2.2.2; SlackBuild for 24.2.2 by Eric Hameleers
FJCC
Moderator
Posts: 9273
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Updating one table with values from another table

Post by FJCC »

To answer the question about the error, one problem is that you are trying to join PILOT_BASIC to itself using the alias PILOT_CERT for both instances of the table. You can't have two tables with the same name. And PILOT_CERT seems to be the name of the table you are trying to update, so you have three things named PILOT_CERT in the query,
Your query:

Code: Select all

    UPDATE PILOT_CERT
    SET
    PILOT_CERT.STREET_1 = PILOT_BASIC.STREET_1
    FROM PILOT_BASIC AS PILOT_CERT
    INNER JOIN PILOT_BASIC AS PILOT_CERT
    WHERE PILOT_CERT.UNIQUE_ID = PILOT_BASIC.UNIQUE_ID
Rather than loading the data from PILOT_BASIC into PILOT_CERT, you should use queries that join the two tables.

Code: Select all

SELECT * 
FROM PILOT_BASIC INNER JOIN PILOT_CERT ON PILOT_BASIC.UNIQUE_ID = PILOT_CERT.UNIQUE_ID
WHERE SOME_CONDITION_IS_MET
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
skysnet
Posts: 5
Joined: Mon Jan 23, 2017 10:21 am

Re: Updating one table with values from another table

Post by skysnet »

Thank you for your replies. Much appreciated.

If I did this

Code: Select all

SELECT * 
FROM PILOT_BASIC INNER JOIN PILOT_CERT ON PILOT_BASIC.UNIQUE_ID = PILOT_CERT.UNIQUE_ID
WHERE N_STATE = CA
Errors:
- The data content could not be loaded.
- The query can not be executed. It contains more than one table.

If I run:

Code: Select all

SELECT * 
FROM PILOT_BASIC INNER JOIN PILOT_CERT ON PILOT_BASIC.UNIQUE_ID = PILOT_CERT.UNIQUE_ID
WHERE 'PILOT_BASIC'.'N_STATE' = 'CA'
Errors:
- The data content could not be loaded.
- Syntax error in SQL expression

and if I do

Code: Select all

    UPDATE PILOT_CERT
    SET
    PILOT_CERT.STREET_1 = PILOT_BASIC.STREET_1
    FROM PILOT_BASIC AS PILOT_CERT
    INNER JOIN PILOT_BASIC AS PILOT_CERT
    WHERE PILOT_CERT.UNIQUE_ID = PILOT_BASIC.UNIQUE_ID
Error:
- The data content could not be loaded.
- syntax error, unexpected '.', expecting SQL_EQUAL
OpenOffice 5.0.4.2 on Windows 10
FJCC
Moderator
Posts: 9273
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Updating one table with values from another table

Post by FJCC »

The error containing "more than one table" makes me think that you are not connected to a fully functional database but to database tables based on text files or spreadsheets. Is that correct? If so, I think you will have to import the data into fully functional tables in order to do joins, which isn't very difficult. I don't have to time to explain all of it at the moment but please explain where your data are stored.

The second query has the error that 'PILOT_BASIC'.'N_STATE' uses single quotes when it should use double quotes or be unquoted.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
skysnet
Posts: 5
Joined: Mon Jan 23, 2017 10:21 am

Re: Updating one table with values from another table

Post by skysnet »

Thank you for your reply.

Tables were originally CSV. I read it is better to re-save them in .dbf format which I did and then loaded into Base.

They are currently showing up under "tables" per attachment

They were brought in by doing -> File -> New Database -> Connect to an existing database -> dBase

If I tried to "open an existing database file" it would say Please choose 'Connect to an existing database' to connect to an existing database instead.
Attachments
2017-01-23 08_52_19-New Database.odb - LibreOffice Base.png
2017-01-23 08_52_19-New Database.odb - LibreOffice Base.png (2.33 KiB) Viewed 6153 times
OpenOffice 5.0.4.2 on Windows 10
skysnet
Posts: 5
Joined: Mon Jan 23, 2017 10:21 am

Re: Updating one table with values from another table

Post by skysnet »

Any tips or suggestions would be much appreciated.
OpenOffice 5.0.4.2 on Windows 10
FJCC
Moderator
Posts: 9273
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Updating one table with values from another table

Post by FJCC »

I don't know about the functionality of dbf files when connected to Base, but your description makes me think that is limiting your ability to do joins. I still think you should get the data into real DB tables. I assume you still have the csv files. Can you open them in Calc and tell us how many lines of data there are? That will help me decide if the method I am considering explaining to you is likely to work.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
skysnet
Posts: 5
Joined: Mon Jan 23, 2017 10:21 am

Re: Updating one table with values from another table

Post by skysnet »

There are over 100K entries. I ended up loading them into SQLLiteStudio and got the results needed that way. Appreciate the help.
OpenOffice 5.0.4.2 on Windows 10
Post Reply