ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
TABLE: first_name
id
name
TABLE: last_name
id
name
TABLE: mapping
firstname_id
lastname_id
How can I map/match first_name.name with last_name.name by mapping table?
I was trying to use INNER JOIN but that fails:
Code:
SELECT first_name.name, last_name.name
FROM mapping
INNER JOIN first_name ON first_name.id = mapping.firstname_id
INNER JOIN last_name ON last_name.id = mapping.lastname_id;
ERROR: table name "mapping" specified more than once
UPDATE:
Possibly that could be a solution. At least by first sight of the result it seems to be what I need:
Code:
SELECT first_name.name, last_name.name
FROM first_name, last_name, mapping
WHERE first_name.id = mapping.firstname_id
AND last_name.id = mapping.lastname_id;
So, no INNER JOIN in that case... although I wonder if there is any more sophisticated way?
I really hope that's a bad attempt at simplifying SQL, because splitting a person's name across three tables is insane.
Plus I'm fairly sure EXPLAIN is going to give the same execution plan for the two queries posted because they're the same, and probably modifying the queries removed whatever syntax issue was causing the error.
Hi Pagonis,
Thank you very much for taking your time to reproduce it
It is actually PostgreSQL where i am trying it, though in my case its version: 11.6 (I dont think that matters).
As a matter of fact, I dont get this error anymore. Instead it gives me the output:
Code:
name | name
------+------
(0 rows)
What I havent mentioned in my first post (and possibly thats the reason), is: firstname_id <> lastname_id.
They are a "couple" in table mapping and are inserted by some logic by the system. But they are not equal.
Hi Pagonis,
Thank you very much for taking your time to reproduce it
It is actually PostgreSQL where i am trying it, though in my case its version: 11.6 (I dont think that matters).
As a matter of fact, I dont get this error anymore. Instead it gives me the output:
Code:
name | name
------+------
(0 rows)
What I havent mentioned in my first post (and possibly thats the reason), is: firstname_id <> lastname_id.
They are a "couple" in table mapping and are inserted by some logic by the system. But they are not equal.
That changes things completely.
Agree with others that even as a simple example the column names are poor choices as they imply that the tables are in need of normalization. With the two keys now being different that becomes even more confusing and meaningless as a real world test case (they are random names and not even first and last names of the same person as the column names would imply).
Additionally, the key names differ in the mapping table and the two name tables, further complicating the queries necessary to use them. But as no actual problem is being solved we can only work with what you have given with the caution to not try to apply it directly to a real world problem.
In general you should not specify those keys in a WHERE clause, but in the JOIN specification itself - that is what the keys are for, otherwise the RDBMS cannot properly optimize the query.
If the keys are different then you will need to join each table with an explicit clause, perhaps like:
Sorry, nothing simpler came to my mind at the time of creating this post.
Replace first_name and lastname_name with X and Y (and the same for columns "name") respectively, if that helps.
I tried JOIN also but it still gives me 0 rows.
Thank you for replies and suggestions.
Initially table first_name, did have column first_name.last_name_id (or X.y_name_id) and mapping table was not there. In such case it was quite simple to do INNER JOIN.
Sadly, after the system upgrade, they decided remove that column and instead create mapping table.
Somehow, under the hood, system creates this mapping table and matched firstname_id and lastname_id (X_id and Y_id).
So far, the only way to match/display these on BD level (outside of the system GUI) is WHERE statement. It seems to be OK, though I verify it only on a few random records.
Last days were chaotic for me.
The JOIN statement does work and it didnt work for me because of my mistake.
I had an error in my query:
Code:
SELECT first_name.name, last_name.name
FROM mapping
JOIN first_name ON(firstname_id=last_name.id) <<<--- should be: first_name.id
JOIN last_name ON(lastname_id=last_name.id);
Once corrected, it works.
Thank you very much for your help.
In case someone wants to reproduce it: https://www.db-fiddle.com/
Code:
CREATE TABLE first_name
(
id SERIAL CONSTRAINT first_name_pk primary key,
name varchar(255) not null
);
# I dot know how to create ID with CONSTRAINT and make it start from 100, so "workedaround" it like this:
CREATE SEQUENCE last_name_id start 100;
CREATE TABLE last_name (
id INT PRIMARY KEY DEFAULT nextval('last_name_id') not null,
name varchar(255) not null
);
CREATE TABLE mapping
(
firstname_id INT CONSTRAINT mapping_first_name_id_fk references first_name,
lastname_id INT
);
insert into first_name(name) values ('f_one'),('f_two');
insert into last_name(name) values ('l_one'),('l_two');
insert into mapping(firstname_id, lastname_id) values ( (SELECT id from first_name where name='f_one'), (SELECT id from last_name where name='l_one') ), ( (SELECT id from first_name where name='f_two'), (SELECT id from last_name where name='l_two') );
SELECT first_name.name, last_name.name
FROM mapping
JOIN first_name ON(firstname_id=first_name.id)
JOIN last_name ON(lastname_id=last_name.id);
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.