Question

so i have 2 tables with the following data

*where a star is a primary key.
*and a # is a unique key.

Table 1

╔══════╦═══════╦════════╗
║ #nlm ║ data2 ║ *data3 ║
╚══════╩═══════╩════════╝

Table 2

╔══════╦═════╗
║ *pid ║ nlm ║
╚══════╩═════╝

The goal is to get data2 when I know pid. I've tried this, and failed badly.

 SELECT data2 FROM table1 LEFT JOIN table2 USING nlm WHERE pid = 3;
Was it helpful?

Solution

Natural join connects two tables using common column names, and it uses the using clause. Other joins use the on clause.

Try this:

SELECT table1.data2
FROM table1 LEFT JOIN
     table2 
     on table1.nlm = table2.`#nlm`
WHERE table2.pid = 3;

OTHER TIPS

The USING clause requires that the column names be in parentheses:

SELECT data2 FROM table1
INNER JOIN table2 USING (nlm)
WHERE pid = 3

This should also be an INNER JOIN, since the WHERE clause requires that it find a row in table2.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top