X
X
xzmyxz2016-05-14 10:06:15
SQL
xzmyxz, 2016-05-14 10:06:15

How to make a selection from several tables in one select?

Guys, please, send me an idea!
It is necessary to display the names of the artists and the genres in which they play, but on the condition that these artists perform their works in more than one genre. How can I check for a condition?
Tables:

CREATE TABLE "artist" (
    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
    "name" TEXT
);
CREATE TABLE "style" (
    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
    "name" TEXT
);
CREATE TABLE "artist_style" (
    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
    "artist_id" INTEGER,
    "style_id" INTEGER,
    FOREIGN KEY ("artist_id") references artist("id"),
    FOREIGN KEY ("style_id") references style("id")
);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2016-05-14
@xzmyxz

if I understood the task correctly, then this:
SELECT artist.id, artist.name, GROUP_CONCAT(style.name), count(artist_style.style_id) as cnt_style
FROM artist
LEFT JOIN artist_style ON artist_style.artist_id = artist.id
LEFT JOIN style ON artist_style .style_id = style.id
GROUP BY artist.id
HAVING cnt_style > 1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question