[英]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. 我对SQL知识还很陌生,而且我所处的知识不足以实现以下目标。
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 : 在Database1上,我运行以下查询:
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 : 在Database2上,我运行以下查询:
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. Database1,CONTENT_ID和Database2 SONG_ID是相同的信息。 And I am not sure if possible, but the output will need to group the artist / song_name as per CONTENT_ID table. 而且我不确定是否可以,但是输出将需要根据CONTENT_ID表将歌手/歌曲名分组。 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
: 将列id
添加到Database1.PROMOTION_LIST
然后创建另一个表,该表将将该id
与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. 让我知道您是否需要进一步的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.