Answer the question
In order to leave comments, you need to log in
How to select multiple tables at once?
Let's say I have a people table with 3 columns: id, age, name. There is a hobby_hunt table, where there are two columns: id, time. I need to get id and name, time for a post with a specific id. (two tables are just related by id)
Answer the question
In order to leave comments, you need to log in
There are 2 tables, of the following form:
-- Запрос на создание таблицы с именем people
CREATE TABLE people (
id INTEGER PRIMARY KEY AUTOINCREMENT,
age INTEGER NOT NULL,
name TEXT NOT NULL
);
-- Запрос на создание таблицы с именем hobby_hunt
CREATE TABLE hobby_hunt (
time_date TEXT NOT NULL,
auth_id INTEGER NOT NULL,
FOREIGN KEY (auth_id) REFERENCES people(id)
);
-- Запрос на добавление данных в таблицу
INSERT INTO people (id, name, age)
VALUES (1, 'Джек Лондон', 40);
INSERT INTO people (id, name, age)
VALUES (2, 'Лев Толстой', 82);
-- Запрос на добавление данных в таблицу
INSERT INTO hobby_hunt (time_date, auth_id)
VALUES ('2019-03-11 21:21:21.000', 1);
INSERT INTO hobby_hunt (time_date, auth_id)
VALUES ('2019-03-11 21:22:22.000', 2);
INSERT INTO hobby_hunt (time_date, auth_id)
VALUES ('2019-03-11 22:22:22.000', 2);
-- SQLite
SELECT
people.id, people.name, hobby_hunt.time_date
FROM
people
INNER JOIN hobby_hunt ON hobby_hunt.auth_id = people.id;
-- SQLite
SELECT
people.id, people.name, hobby_hunt.time_date
FROM
people
INNER JOIN hobby_hunt ON hobby_hunt.auth_id = people.id WHERE hobby_hunt.auth_id = 2;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question