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.
tblPeople
ID numeric
countryid numeric
(more fields)
tblCountries has all the names of the countries, someone lives in a country. Database normalisation dictates that data should NOT repeat itself, hence the need for a table where countries are listed, and a link (tblCountries.ID - tblPeople.countryid). There are no database links, the link is fictive. I do not like to make links the way access did, that's too rigid...I let the software/SQL handle that.
How to I list the country (among other fields) of the people from the countryid?
I understand this could be a join, but joins have always been illusive to me.
Thanks for shedding some light. Of course, if there's a link to a good tutorial, I'll have learned to fish, and I'll eat for the rest of my life
Thor
Click here to see the post LQ members have rated as the most helpful post in this thread.
SELECT DISTINCT co.name as CountryName, pp.id as PersonID
FROM tblPeople pp
INNER JOIN tblCountries co ON (pp.countryID = co.ID)
The concept is simple ... keep it that way.
When you write a SELECT, you can draw information from as many tables as you wish. The only thing that you need to do is to specify what the various tables have in common. The database manager will match-up the rows from the various sources, filter it down to only DISTINCT rows if you wish, then select from the matched-up rows.
Be aware that if, say, the "left side" table contains (say) 2 occurrences of a particular value, and the "right side" table contains (say) 3 occurrences, the result-set will contain "2 * 3 = 6" rows having that value. It can add up very fast.
Normally, the join is an "inner" join. That is, it includes only those rows which exist in both tables. But you can also have "outer" joins, either "left" or "right." A "left" outer-join always includes all of the rows from the table on the "left" side of the join whether or not those rows have matching rows in the table on the "right" side, and vice-versa.
Notice also that it is possible to join tables without specifying an ON (or, in mySQL, USING...) clause, in which case you get a cartesian product: every row matched with every row. (Not a good thing if both tables contain 50,000 rows each. "You asked for it. You got it. All 2,500,000,000 rows of it. Your system died, of course, but... you asked for it, and you got it.")
Last edited by sundialsvcs; 03-13-2012 at 05:29 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.