4

Im trying to learn how to match two files together. But I'm trying everythng for 5 hours now... And still no idea what to do.

The first file(600.000 rows) contains 4 columns:

Postal, Number, Houseletter, livingspace

The second file(7.000 rows) contains 4 columns:

Postal, Number, Houseletter, Furniturevalue

In my first file I have all the livingspaces from a big area and in my second file I have the Furniturevalue of a couple of adresses in that big area.

I want to add to the adresses in my second file the livingspaces from file 1.

So I imported the files in a database.

Table first file -> Space
Table second file -> Furniture

Now i'm trying to make Primary keys to the tables:

Primary key --> Postal, Number, Houseletter

But this doesn't work, because the columns are only unique when Postal+Number+Houseletter, but not apart from each other.

Does anyone know the next step? What do I have to do to make this query work:

SELECT postal, number, houseletter, furniturevalue, livingspace
FROM space, furniture
WHERE ( space.postal = furniture.postal
AND     space.number = furniture.number
AND     space.houseletter = furniture.houseletter)

Im trying to make with this query a new view with `postal, number, houseletter, furniturevalue, livingspace' So data from two tables. But first I need a solution for my problem with the primary key.

Thanks for your help!

ps: Im using sql in phpmyadmin

4
  • what do you mean by Primary key --> Postal, Number, Houseletter did you try to make three primary keys or a composed primary key using the three columns ? Commented Mar 29, 2013 at 16:03
  • Did you try creating composite primary keys? And do you get any errors when you run your SQL?
    – rs.
    Commented Mar 29, 2013 at 16:03
  • You should realize what you are doing is the opposite of a 'best practice'. Commented Mar 29, 2013 at 16:06
  • No I just wanted to let you guys know that that are my primary keys.
    – warnerst
    Commented Mar 29, 2013 at 16:09

4 Answers 4

9
ALTER TABLE space ADD PRIMARY KEY(Postal, Number, Houseletter);

If a primary key already exists then you want to do this:

ALTER TABLE space DROP PRIMARY KEY, ADD PRIMARY KEY(Postal, Number, Houseletter);

if you got duplicate PKs, you can try this:

ALTER IGNORE TABLE space ADD UNIQUE INDEX idx_name (Postal, Number, Houseletter );

This will drop all the duplicate rows. As an added benefit, future INSERTs that are duplicates will error out. As always, you may want to take a backup before running something like this

Second question, your query should look like this :

SELECT postal, number, houseletter, furniturevalue, livingspace
FROM space INNER JOIN furniture
ON ( space.postal = furniture.postal
AND     space.number = furniture.number
AND     space.houseletter = furniture.houseletter)
8
  • thanks! I did the first query but I got an error that some rows are double.. But Do you know to delete double rows?
    – warnerst
    Commented Mar 29, 2013 at 16:11
  • Thanks I will try, I let you know!
    – warnerst
    Commented Mar 29, 2013 at 16:21
  • Can you invite me for a chat? I have a view questions!
    – warnerst
    Commented Mar 29, 2013 at 16:37
  • What do I have to type here? 'idx_name'
    – warnerst
    Commented Mar 29, 2013 at 16:39
  • I got an error when quering this: ALTER IGNORE TABLE space ADD UNIQUE INDEX idx_name (Postal, Number, Houseletter ); My error is: #1062 - Duplicate entry '1391BR-19-' for key 'Adres_index'
    – warnerst
    Commented Mar 29, 2013 at 16:43
1

You can use group by to create a unique row per (postal, number, houseletter) combination. An aggregate calculates the value of livingspace for that group. This example picks the highest livingspace with the max() aggregate:

select  coalesce(space.postal, furniture.postal) as postal
,       coalesce(space.number, furniture.number) as number
,       coalesce(space.houseletter, furniture.houseletter) as houseletter
,       max(furniture.furniturevalue) as furniturevalue
,       max(space.livingspace) as livingspace
from    space
full outer join
        furniture
on      space.postal = furniture.postal
        and space.number = furniture.number
        and space.houseletter = furniture.houseletter
group by
        coalesce(space.postal, furniture.postal)
,       coalesce(space.number, furniture.number)
,       coalesce(space.houseletter, furniture.houseletter)
2
  • Thanks I will try and let you know!
    – warnerst
    Commented Mar 29, 2013 at 16:29
  • I got this, when adding the primary keys: #1062 - Duplicate entry '1391BR-19-' for key 'Adres_index'
    – warnerst
    Commented Mar 29, 2013 at 16:41
0
ALTER TABLE <TableName>
ADD CONSTRAINT <constraintName> PRIMARY KEY (colA, colB, ..., colN)
0

Something like this might work. You can use concat() to combine the columns together.

select (concat(s.Postal,' ',s.Number,' ',s.Houseletter)) as s.address,(concat(f.Postal,' ',f.Number,' ',f.Houseletter)) as f.address,s.*,f.* from space s,furniture f where s.address=f.address;

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.