Hello, hope this is a simple sql-problem. I'm not an sql-champion but do a C++ project and need urgently quick help and hope do be clear enough to form a good question:
Now I need to retrieve the clearnames of the companies in hopefully one request.
I can do a simple JOIN and a SELECT to retrieve one of both names. But how is the syntax for creating a query containing two times the same column but with different search conditions?
Hallo Guys, hallo Scott once again. I tried out your ideas. somehow both do work. ...except: While I'm not really familiar with sql yet I don't know what exactely it is, but I try something like this:
SELECT... // somecolums // ...c1.Institution, c2.Institution
FROM produktionp, requestsr, contactsc1, contactsc2
WHERE p.P_ID = r.P_ID // pick some specific productionIDs
AND c1.K_ID = r.fromK_ID
AND c2.K_ID = r.toK_ID
now it almost works. except when the fromK_ID or toKID field is empty, the whole job is not found.
SELECT ISNULL(c_from.company, '') + ' | ' + ISNULL(c_to.company, '') AS from_to_company
FROM requests r
LEFT OUTER JOIN companies c_from ON c_from.ID = r.fromComp
LEFT OUTER JOIN companies c_to ON c_to.ID = r.toComp
You can avoid the ISNULL function by using the new CONCAT function...which handles that for you.
SELECT CONCAT(c_from.company, ' | ', c_to.company) AS from_to_company
FROM requests r
LEFT OUTER JOIN companies c_from ON c_from.ID = r.fromComp
LEFT OUTER JOIN companies c_to ON c_to.ID = r.toComp
You really should start utilization the INNER/LEFT/RIGHT JOIN syntax - it makes the queries much easier to read and much easier to determine how the tables are related.
SELECT... // somecolums // ...c1.Institution, c2.Institution
FROM produktion p, requests r, contacts c1, contacts c2
WHERE p.P_ID = r.P_ID // pick some specific productionIDs
AND c1.K_ID = r.fromK_ID
AND c2.K_ID = r.toK_ID
Should be rewritten as:
SELECT ... // somecolums // ...c1.Institution, c2.Institution
FROM produktion p
INNER JOIN requests r ON r.P_ID = p.P_ID
LEFT JOIN contacts c1 ON c1.K_ID = r.fromK_ID
LEFT JOIN contacts c2 ON c2.K_ID = r.toK_ID
In fact - you cannot utilize the old style outer join syntax anyways as it has been deprecated.
Doing so, the programm I'm on (libreOffice) understands, but rewrites it in Scott's style mixed up with what you call old style. As I wrote in a former statement, I'm working on an old and dirty accessdB on an old and dirty winNT4.0 virtual machine. So,...what I need is a dirty solution. And a drink.
Thanks for guiding me so far. You're great.
Lars
okay guys, did some homework. Seems, that my database doesn't support full outer join. Scott was about right at first sight. I did a workaround creatimg
A left join B
Union
B left join A
Thanks a Million for all the guidance and patience.