无法通过查询从三个表中提取数据

时间:2016-05-19 14:36:15

标签: mysql sql database

我正在尝试编写MySQL查询以从三个不同的表中检索数据,这些表之间没有已定义的关系。

FacebookMusicStars有来自Facebook的名人名字。

TwitterMusicStars有来自Twitter的名人名字。

YouTubeMusicStars有YouTube名人的名字。

问题是我无法在同一个表中按名称匹配它们,因为它们的名称在所有三个表中都不相同。我使用的查询:

SELECT f.musicNameFacebook
     , t.musicNameTwitter
     , y.musicNameYoutube
  FROM facebookmusicstars f
  LEFT 
  JOIN twittermusicstars t
    ON f.musicNameFacebook LIKE t.musicNameTwitter
  LEFT 
  JOIN youtubemusicstars y
    ON f.musicNameFacebook LIKE y.musicNameYoutube
LEFT JOIN youtubemusicstars ON facebookmusicstars.musicNameFacebook LIKE youtubemusicstars.musicNameYoutube

enter image description here

1 个答案:

答案 0 :(得分:0)

我使用的是Windows命令行和MySQL 5.5 - 在字符集方面,它们似乎并不是很好地协同工作。使用PowerCMD和Latin1,我可以得到以下内容,但如果您想要考虑其他字符(' @'等),那么您需要明确地编写代码...

SET names latin1;

DROP TABLE IF EXISTS facebookmusicstars;
CREATE TABLE facebookmusicstars 
(musicNameFacebook VARCHAR(30) NOT NULL PRIMARY KEY) DEFAULT CHARSET=latin1;

DROP TABLE IF EXISTS twittermusicstars;
CREATE TABLE twittermusicstars 
(musicNameTwitter VARCHAR(30) NOT NULL PRIMARY KEY) DEFAULT CHARSET=latin1;

DROP TABLE IF EXISTS youtubemusicstars;
CREATE TABLE youtubemusicstars 
(musicNameYoutube VARCHAR(30) NOT NULL PRIMARY KEY) DEFAULT CHARSET=latin1;


INSERT INTO facebookmusicstars VALUES
('Rihanna'), 
('Eminem'), 
('Shakira'), 
('Pitbull'), 
('Beyoncé'), 
('Wiz Khalifa'), 
('Adele'), 
('Bruno Mars'), 
('Ed Sheeran'), 
('MattyBRaps'), 
('Jason Derulo'), 
('Drake'), 
('MALUMA'), 
('Flo Rida'), 
('Usher'), 
('AKON'), 
('Michel Teló'), 
('Nelly'), 
('INNA'), 
('Farruko'), 
('Ciara'), 
('Trey Songz'), 
('Kesha'), 
('Anitta'), 
('Tyga');

INSERT INTO twittermusicstars VALUES
('Rihanna'), 
('Eminem'), 
('Shakira'), 
('pitbull'), 
('Beyonce'), 
('Adèle'), 
('50 Cent'), 
('Lil Wayne'), 
('Ne-yo'), 
('Drake'), 
('Snoop Dogg'), 
('FettyWap'), 
('Usher'), 
('Akon'), 
('Tyga');

INSERT INTO youtubemusicstars VALUES
('Eminem'), 
('Shakira'), 
('Pitbull'), 
('beyonce'), 
('Wiz Khalifa'), 
('Adele'), 
('Bruno Mars'), 
('Ed Sheeran'), 
('MattyBRaps'), 
('Jason Derulo'), 
('Drake'), 
('Maluma'), 
('Flo Rida'), 
('Usher'), 
('Akon'), 
('Michel Telo'), 
('Nelly'), 
('INNA'), 
('FARRUKO'), 
('Ciara'), 
('Trey Songz'), 
('kesha'), 
('Anitta'), 
('Tyga');
Query OK, 0 rows affected (0.00 sec)

SELECT f.musicNameFacebook
     , t.musicNameTwitter
     , y.musicNameYoutube
  FROM 
     ( SELECT musicNameFacebook musicName
         FROM facebookmusicstars 
        UNION
       SELECT musicNameTwitter
         FROM twittermusicstars 
        UNION
       SELECT musicNameYoutube
         FROM youtubemusicstars 
     ) x 
  LEFT
  JOIN facebookmusicstars f
    ON f.musicNameFacebook = x.musicName
  LEFT 
  JOIN twittermusicstars t
    ON t.musicNameTwitter  = x.musicName
  LEFT 
  JOIN youtubemusicstars y
    ON y.musicNameYoutube  = x.musicName;

+-------------------+------------------+------------------+
| musicNameFacebook | musicNameTwitter | musicNameYoutube |
+-------------------+------------------+------------------+
| Adele             | Adèle            | Adele            |
| AKON              | Akon             | Akon             |
| Anitta            | NULL             | Anitta           |
| Beyoncé           | Beyonce          | beyonce          |
| Bruno Mars        | NULL             | Bruno Mars       |
| Ciara             | NULL             | Ciara            |
| Drake             | Drake            | Drake            |
| Ed Sheeran        | NULL             | Ed Sheeran       |
| Eminem            | Eminem           | Eminem           |
| Farruko           | NULL             | FARRUKO          |
| Flo Rida          | NULL             | Flo Rida         |
| INNA              | NULL             | INNA             |
| Jason Derulo      | NULL             | Jason Derulo     |
| Kesha             | NULL             | kesha            |
| MALUMA            | NULL             | Maluma           |
| MattyBRaps        | NULL             | MattyBRaps       |
| Michel Teló       | NULL             | Michel Telo      |
| Nelly             | NULL             | Nelly            |
| Pitbull           | pitbull          | Pitbull          |
| Rihanna           | Rihanna          | NULL             |
| Shakira           | Shakira          | Shakira          |
| Trey Songz        | NULL             | Trey Songz       |
| Tyga              | Tyga             | Tyga             |
| Usher             | Usher            | Usher            |
| Wiz Khalifa       | NULL             | Wiz Khalifa      |
| NULL              | 50 Cent          | NULL             |
| NULL              | FettyWap         | NULL             |
| NULL              | Lil Wayne        | NULL             |
| NULL              | Ne-yo            | NULL             |
| NULL              | Snoop Dogg       | NULL             |
+-------------------+------------------+------------------+