How to mix SQL queries

0

I have the following tables:

series_trailers :

ID EPISODEID  CONTENT  AUTHOR
-----------------------------
1  122383    url1     Peter
2  9999      url2     Ana
3  923822    stuff    Jhon
4  122384    url3     Drake

series_episodes :

ID      TITLE           SERIESID
--------------------------------
122383  Episode 1       23
9999    Somethingweird  87
923822  Randomtitle     52
122384  Episode 2       23

series :

ID   TITLE
-------------------
23   Stranger Things
87   Seriesname
512  Sometrashseries

I want to get the latest series updated (with updated I mean the last series to which a url has been added to see the trailer of an episode). Each URL in series_trailers is associated with an episode of series_episodes and each episode a series , if I use the following code: SELECT DISTINCT EPISODEID FROM series_trailers ORDER BY id DESC I will get the following:

  • Stranger Things
  • Seriesname
  • Sometrashseries
  • Stranger Things

As you can see, I would get the Stranger Things series twice, and that's what I do not want. I know it should be with a sql code that combines several but I am not very aware of the subject, if someone can help I would be very grateful: -)

    
asked by Ismael Quirantes 27.01.2018 в 22:10
source

2 answers

1

There are at least two possibilities to solve this.

1. Using GROUP BY :

SELECT s.TITLE FROM series_episodes se
INNER JOIN series s ON se.SERIESID = s.ID
INNER JOIN series_trailers st ON se.ID = st.EPISODEID 
GROUP BY s.ID
ORDER BY se.ID DESC;

2. Using DISTINCT :

SELECT DISTINCT s.TITLE FROM series_episodes se
INNER JOIN series s ON se.SERIESID = s.ID
INNER JOIN series_trailers st ON se.ID = st.EPISODEID
ORDER BY se.ID DESC;

The result will always be the same.

Seeing the table series_trailers , it is quickly understood that you need to get the name of the author from it, creating a table dedicated solely to the authors. Otherwise, you will be forced to repeat a VARCHAR column several times. And if you also need to know that information for some reports, it will be much more complicated.

Let's see some tests and their results now.

SEE DEMO IN REXTESTER

Tables and data entered for the test

    CREATE TABLE IF NOT EXISTS series_trailers 
    (
        ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY, 
        EPISODEID INT,
        CONTENT VARCHAR(255),
        AUTHORID INT

    )ENGINE=INNODB;

    INSERT INTO series_trailers (EPISODEID,CONTENT,AUTHORID)
        VALUES 
        (122383,'url1',  1),
        (9999,  'url2',  2),
        (923822,'stuff', 3),
        (122384,'url3',  4)
    ;

   /*Puede que el ID de esta tabla sea auto-incremental*/ 
   CREATE TABLE IF NOT EXISTS series_episodes 
    (
        ID INT NOT NULL PRIMARY KEY, 
        TITLE VARCHAR(255),
        SERIESID INT

    )ENGINE=INNODB;

    INSERT INTO series_episodes (ID,TITLE,SERIESID)
        VALUES 
        (122383,'Episode 1',23),
        (9999,  'Somethingweird',87),
        (923822,'Randomtitle',52),
        (122384,'Episode 2',23)
    ;


    /*Puede que el ID de esta tabla sea auto-incremental*/ 
    CREATE TABLE IF NOT EXISTS series 
    (
        ID INT NOT NULL PRIMARY KEY, 
        TITLE VARCHAR(255)

    )ENGINE=INNODB;    

    INSERT INTO series (ID, TITLE)
        VALUES 
        (23,'Stranger Things'),
        (87,'Seriesname'),
        (512,'Sometrashseries') 
    ;

   CREATE TABLE IF NOT EXISTS authors 
    (
        ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY, 
        AUTHOR VARCHAR(255)

    )ENGINE=INNODB;  

    INSERT INTO authors (AUTHOR)
        VALUES 
        ('Peter'),
        ('Anna'), 
        ('Jhon'),
        ('Drake') ;

Test queries

   -- Con GROUP BY
SELECT s.TITLE FROM series_episodes se
INNER JOIN series s ON se.SERIESID = s.ID
INNER JOIN series_trailers st ON se.ID = st.EPISODEID 
GROUP BY s.ID
ORDER BY se.ID DESC;

  -- Con GROUP BY, incluyendo JOIN de AUTHORS
SELECT * FROM series_episodes se
INNER JOIN series s ON se.SERIESID = s.ID
INNER JOIN series_trailers st ON se.ID = st.EPISODEID 
INNER JOIN authors a ON a.ID=st.AUTHORID
GROUP BY s.ID
ORDER BY se.ID DESC;    

  -- Con DISTINCT
SELECT DISTINCT s.TITLE FROM series_episodes se
INNER JOIN series s ON se.SERIESID = s.ID
INNER JOIN series_trailers st ON se.ID = st.EPISODEID
ORDER BY se.ID DESC;

Results

-- Con GROUP BY

TITLE
Stranger Things
Seriesname

-- Con GROUP BY incluyendo JOIN de AUTHORS y todas las columnas

ID      TITLE           SERIESID    ID  TITLE               ID      EPISODEID   CONTENT     AUTHORID    ID  AUTHOR
122383  Episode 1       23          23  Stranger Things     1       122383      url1        1           1   Peter
9999    Somethingweird  87          87  Seriesname          2       9999        url2        2           2   Anna


-- Con DISTINCT

TITLE
Stranger Things
Seriesname
    
answered by 28.01.2018 в 22:16
0
SELECT DISTINCT TITLE FROM series s LEFT JOIN series_episodies e ON s.id = e.seriesid LEFT JOIN series_trailer t on e.id = t.episodieid ORDER BY t.id DESC;

or you remove the distinct and you do a group by id, so what you do is to choose the title without repeating

    
answered by 27.01.2018 в 23:43