selecting records from multiple files in mysql question
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.
But what i want is that the Table1 be considered as main table and that all the fields from there is loaded and for the values that are not found in Table2 the value 'Link value Not Found' is displayed
Like this:
Code:
FLDS flds
Link Value not Found flds1
fs2 flds2
Link Value not Found flds3
fs4 flds4
fs5 flds5
fs6 flds6
Link Value not Found flds7
fs8 flds8
Link Value not Found flds9
Thanks for your answer, but thats not it... this returns the same values as before. all the values that are not found in Table2 are not displayed. I want to display all from Table1 even if the corresponding link can't be found in Table2. This is important when displaying financial figures from Table1 and requesting only a description from Table2.
what i want is that the Table1 be considered as main table and that all the fields from there is loaded and for the values that are not found in Table2 the value 'Link value Not Found' is displayed
That's commonly called an outer join, and normally would be formulated like this,
Code:
select Table2.FLDS, Table1.flds from Table1, Table2 where Table2.PKEY (+) = Table1.pkey
Note that (+) that I added. Not sure if MySql understands that syntax, as I don't have it installed here. Could you try it? It will give you NULL values (i.e., the empty string) where table2 doesn't have a corresponding row. To get the specific string you want, I'd use NVL, which I guess is Oracle specific; substitute the appropriate MySql function. Also, Oracle would put all the rows that exist in table2 first; to order them as you requested, add an ORDER BY clause:
Code:
select nvl(table2.flds, 'Link Value not Found') flds, table1.flds
from table1, table2
where table2.pkey (+) = table1.pkey
order by table1.pkey;
In Oracle's sqlplus, the above query (with your tables) gives:
Code:
FLDS FLDS
-------------------- -----
Link Value not Found flds1
fs2 flds2
Link Value not Found flds3
fs4 flds4
fs5 flds5
fs6 flds6
Link Value not Found flds7
fs8 flds8
Link Value not Found flds9
9 rows selected.
select Table2.FLDS, Table1.flds from Table1, Table2 where Table2.PKEY (+) = Table1.pkey
I've not seen that sytax before now. Interesting. . .
However, I am pretty sure MySQL will do the Left Outer Join as follows:
Code:
select Table2.FLDS, Table1.flds from Table1 left outer join Table2 on Table1.PKEY = Table2.PKEY
Quote:
Originally Posted by Quigi
Note that (+) that I added. Not sure if MySql understands that syntax, as I don't have it installed here. Could you try it? It will give you NULL values (i.e., the empty string) where table2 doesn't have a corresponding row. To get the specific string you want, I'd use NVL, which I guess is Oracle specific; substitute the appropriate MySql function. Also, Oracle would put all the rows that exist in table2 first; to order them as you requested, add an ORDER BY clause:
Code:
select nvl(table2.flds, 'Link Value not Found') flds, table1.flds
from table1, table2
where table2.pkey (+) = table1.pkey
order by table1.pkey;
I'm not sure of the syntax to replace NULLs in MySQL, but it's probably something very similar to Oracle's NVL () function. Should be easy to look up, though.
Hi Thanks Everybody! I Finally was able to get the result that i wanted by doing this command:
'select IFNULL(Table2.FLDS, 'Link Value not Found') FLDS, Table1.flds from Table1 left outer join Table2 on Table1.pkey = Table2.PKEY'
NVL() is replaced by IFNULL()
the ' (+) = ' does not work in MySQL, it returns a syntax error
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.