简体   繁体   中英

Mysql - Combine two select queries, from two different databases in just one output

I am quite new to SQL stuff, and I am in situation where I don't have enough knowledge to achieve the following.

I am trying to combine the results of two select queries, but one query is from one data base and one for another . Both data bases are in the same server.

Like below :

On Database1 I run the query below :

MariaDB [(none)]> select GENRE,STYLE,CONTENT_ID,PROMO_END_DATE from Database1.PROMOTION_LIST ;
+-----------+---------------------------------------+-------------------------+----------------------+
| GENRE     | STYLE                                 | CONTENT_ID              |PROMO_END_DATE        |
+-----------+---------------------------------------+-------------------------+----------------------+
| ROCK      |Hard Rock,Opera Rock, Syphonic Rock    | 11111,22222,33333       | 2015-12-02 04:00:00  |
| METAL     |Black Metal, Death Metal, Heavy Metal  | 55555,66666,77777,22222 | 2015-12-29 11:00:00  |
| ELECTRO   |Dance, Tance, Flash House              | 88888,22222,66666,44444 | 2015-12-02 07:00:00  |
+-----------+-------------+---------------------------------------------------+----------------------+

On Database2 I run the query below :

SELECT SONG_ID,SONG_NAME,ARTIST FROM Database2.song_master_table

+---------+---------------+---------+
| SONG_ID | SONG_NAME     | ARTIST  |
+---------+---------------+---------+
| 11111   | SONG1         | BAND1   |
| 22222   | SONG2         | SINGER1 |
| 33333   | SONG3         | ARTIST1 |
| 44444   | SONG4         | BAND2   |
| 55555   | SONG5         | SINGER2 |
| 66666   | SONG6         | ARTIST2 |
| 77777   | SONG7         | BAND2   |
| 88888   | SONG8         | SINGER2 |
+---------+---------------+---------+

And the output I am trying to achieve is :

+-----------+---------------------------------------+-------------------------+--------------+---------------+--------------------+
| GENRE     | STYLE                                 | CONTENT_ID              |     ARTIST   | SONG_NAME     | PROMO_END_DATE     |
+-----------+---------------------------------------+-------------------------+--------------+---------------+--------------------+
| ROCK      |Hard Rock,Opera Rock, Syphonic Rock    | 11111                   | BAND1        | SONG1         | 2015-12-02 04:00:0 |
|           |                                       | 22222                   | SINGER1      | SONG2         | 2015-12-02 04:00:0 |
|           |                                       | 33333                   | ARTIST1      | SONG3         | 2015-12-02 04:00:0 |
| METAL     |Black Metal, Death Metal, Heavy Metal  | 55555                   | SINGER2      | SONG5         | 2015-12-02 04:00:0 |
|           |                                       | 66666                   | ARTIST2      | SONG6         | 2015-12-02 04:00:0 |
|           |                                       | 77777                   | BAND2        | SONG7         | 2015-12-02 04:00:0 |
|           |                                       | 22222                   | SINGER1      | SONG2         | 2015-12-02 04:00:0 |
|           |                                       | 33333                   | ARTIST1      | SONG3         | 2015-12-02 04:00:0 |
| ELECTRO   |Dance, Tance, Flash House              | 88888                   | SINGER2      | SONG8         | 2015-12-02 04:00:0 |
|           |                                       | 22222                   | SINGER1      | SONG2         | 2015-12-02 04:00:0 |
|           |                                       | 66666                   | ARTIST2      | SONG6         | 2015-12-02 04:00:0 |
|           |                                       | 44444                   | BAND2        | SONG4         | 2015-12-02 04:00:0 |
+-----------+---------------------------------------+-------------------------+--------------+---------------+--------------------+

I tried to use join and union, but the syntax were not correct.

Consider that :

Database1,CONTENT_ID and Database2 SONG_ID are the same info. And I am not sure if possible, but the output will need to group the artist / song_name as per CONTENT_ID table. And there is no where condition.

I appreciate all and any help.

Thanks in advanced.

Add column id to Database1.PROMOTION_LIST then create another table which will connect this id with SONG_ID :

promo_id   song_id
1           11111
1           22222
1           33333
2           55555
...

Then it will be much easier to create the query you need.

Let me know if you need further help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM