Gergo Szucs
Gergo Szucs

Reputation: 105

Best way to join multiple tables

My question would be the following.

I need to query like 15 columns from different tables. Which is the best way to do that. For example, i wrote a query that selects the guid (unique ID for every DynFlow process), the last date modified for this guid, the time spent on the process with this guid, the last modifier's name. So now i have 16 rows, 5 columns, with the guid as unique ID. These all come from the same table.

Now i have a different table, that stores data about each step taken in DynFlow. Guid is provided for them aswell. My problem is, that most of the data i need to provide for every guid from the previous query is not present in these tables. For example, partnerName is not given to any of these guids, customerID is given to half of them, each resulting in different numbers of rows.

So, to give you an example, i have 16 rows from the first query, and 12 rows (these 12 rows have the same guids as the first query) from the second query, from the other table. How could i join them, so the ones that have matched guid can take the customerID, and the others would be filled with 'none' or something?

Upvotes: 0

Views: 1901

Answers (2)

SEB BINFIELD
SEB BINFIELD

Reputation: 410

You will need to use the left join.

The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match.

The left table is always the name of the table before the 'left join' keyword.

Basic syntax=

SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;

Upvotes: 1

bowlturner
bowlturner

Reputation: 2016

As Dimt pointed out look up the use of LEFT JOIN (or even all OUTER JOIN usages LEFT, RIGHT and FULL) that is how you get rows back from a table where not all rows match in a join. If you have any problems using it, come back with a more specific question.

Upvotes: 1

Related Questions