[英]mysql left join and inner join 3 tables
I have 3 tables: oc_artists, oc_songs, oc_songs_tags我有 3 个表:oc_artists、oc_songs、oc_songs_tags
I can select 2 tables with this code:我可以使用此代码选择 2 个表:
SELECT * FROM `oc_songs` LEFT JOIN oc_songs_tags ON oc_songs.song_id=oc_songs_tags.song_id
WHERE oc_songs_tags.song_tag IS NULL
Now I need to select data from oc_artists... I tried this questions answer: MySQL LEFT JOIN 3 tables现在我需要从 oc_artists 中选择数据...我试过这个问题的答案: MySQL LEFT JOIN 3 tables
And here is my code:这是我的代码:
SELECT * FROM oc_songs
LEFT JOIN oc_artists
INNER JOIN oc_songs_tags
ON oc_songs.song_artist_id = oc_artists.artist_id
ON oc_songs_tags.song_id = oc_songs.song_id
But I am getting this error但我收到此错误
8 errors were found during analysis.
Unrecognized keyword. (near "ON" at position 131)
Unexpected token. (near "oc_songs_tags" at position 134)
Unexpected token. (near "." at position 147)
Unexpected token. (near "song_id" at position 148)
Unexpected token. (near "=" at position 156)
Unexpected token. (near "oc_songs" at position 158)
Unexpected token. (near "." at position 166)
Unexpected token. (near "song_id" at position 167)
Your syntax is wrong.你的语法是错误的。 Join syntax is:连接语法是:
[JOIN TYPE] JOIN [TABLE B] ON [TABLE A].[COLUMN] = [TABLE B].[COLUMN]
So that would mean:所以这意味着:
SELECT * FROM oc_songs
LEFT JOIN oc_artists ON oc_songs.song_artist_id = oc_artists.artist_id
INNER JOIN oc_songs_tags ON oc_songs.song_id = oc_songs_tags.song_id
The simple way to understand JOINS is that they are ALWAYS a conjunction between the primary table listed in FROM and the table you are JOINING.理解 JOINS 的简单方法是它们始终是 FROM 中列出的主表和您要 JOINING 的表之间的连接。 So think of the JOIN clause as a list of conjunctions.因此,将 JOIN 子句视为连词列表。
So to join TableA with TableB, TableC, and TableD you might have something like:因此,要将 TableA 与 TableB、TableC 和 TableD 连接起来,您可能有以下内容:
SELECT * FROM TableA
JOIN TableB on TableA.tableb_id = TableB.id -- this is first join
JOIN TableC on TableA.tablec_id = TableC.id -- this is second join
JOIN TableD on TableA.tabled_id = TableD.id -- this is third join
Wrong position of ON clause ON 子句的错误位置
SELECT * FROM oc_songs
LEFT JOIN oc_artists ON oc_songs.song_artist_id = oc_artists.artist_id
INNER JOIN oc_songs_tags ON oc_songs_tags.song_id = oc_songs.song_id
Example tables:示例表:
DROP TABLE IF EXISTS `oc_songs` ;
DROP TABLE IF EXISTS `oc_songs_tags` ;
DROP TABLE IF EXISTS `oc_artists` ;
CREATE TABLE `oc_songs` (
`song_id` int(10) unsigned NOT NULL auto_increment,
`song_name` varchar(255) NOT NULL,
`song_artist_id` int(10),
PRIMARY KEY (`song_id`),
foreign key (`song_artist_id`) references oc_artists(`artist_id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
CREATE TABLE `oc_songs_tags` (
`song_tag_id` int(10) unsigned NOT NULL auto_increment,
`song_tag` varchar(255) NOT NULL,
`song_id` int(10),
PRIMARY KEY (`song_tag_id`),
foreign key (`song_id`) references oc_songs(`song_id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
CREATE TABLE `oc_artists` (
`artist_id` int(10) unsigned NOT NULL auto_increment,
`artist_age` int(4),
PRIMARY KEY (`artist_id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
Right select:右键选择:
SELECT * FROM oc_songs
LEFT JOIN oc_artists ON
oc_songs.song_artist_id = oc_artists.artist_id
INNER JOIN oc_songs_tags ON
oc_songs.song_id = oc_songs_tags.song_id;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.