tags:

views:

63

answers:

2

Hello.

Sorry for novice question, i have a really limited experience working with SQL :(. I have two tables. First one:

create table history ( id integer primary key, task integer )

And second one:

create table task ( id integer primary key, name text )

What will be an SQLite query that will return a list of history.id and task.name where history.task == task.id ( all items from 'history' table with 'name's from 'task' table )?

+1  A: 

Try the following:

SELECT history.id, task.name FROM history LEFT JOIN task ON history.task = task.id
adam
What's the difference between inner join and left join in my case?
Eye of Hell
A left join will show all history ids regardless of whether the row has a matching task. An inner join will only show where both are present.
adam
Ah, it's intersection rules, thanks :)
Eye of Hell
+2  A: 
SELECT history.id, task.name
    FROM history
    INNER JOIN task
    WHERE history.task = task.id

Use a LEFT JOIN if you aren't sure that the task will be present for a particular row in history - nulls will be returned if the row is missing.

The syntax for SELECT is described here (using pictures).

Mark Byers
What's the difference between inner join and left join in my case?
Eye of Hell
If you only want rows returned that have both a history and a task then you should use INNER JOIN. If you don't mind getting back rows that have a history but no task, then use a LEFT JOIN.
Mark Byers