我有一个MySQL数据库,有三个表:艺术家、流派和genre_artist。我需要一个MySQL查询,它为我提供了一个特定类型的所有艺术家。让我们说“摇滚”。我在加入MySQL表格方面没有那么丰富的经验,所以有人能帮我吗?
我的表行如下:
艺术家:
artist_id (int, ai, pk)
artist_name(varchar)
artist_image(varchar)体裁:
id, (int, ai, pk)
name (varchar)
parent_id (int)genre_artist:
genre_artist_id (int, ai, pk)
genre_id (int)
artist_id (int)有谁能帮我吗?
提前谢谢..。
发布于 2014-02-18 20:10:59
SELECT artists.*
FROM artists join genre_artist on genre_artist.artist_id = artists.artist_id
join genre on genre.id = genre_artist.genre_id and genre.name = 'Rock';发布于 2014-02-18 20:08:14
SELECT a.*
FROM
`artists` a
INNER JOIN `genre_artist` ga ON ga.artist_id = a.artist_id
INNER JOIN `genre` g ON g.genre_id = ga.genre_id
WHERE
g.name = 'Rock'
;发布于 2014-02-18 20:09:53
假设您在$genreId中有rock id,您可以使用一个子查询
$genre_id = mysql_real_escape_string($genreId); // basic protection
$subquery = "SELECT artist_id FROM genre_artist WHERE genre_id = {$genre_id}";
$query = "SELECT * FROM artist WHERE artist_id IN ({$subquery})";联接对于数据库来说可能是非常有意义的。如果您没有添加正确的indice,并且在错误的模式下使用thge表,则可以将表锁定的时间比所需的时间长,这会造成缓慢的性能。注意: Subqueries的可能性较小,mysql的旧版本不支持它(不知道最近的版本)
https://stackoverflow.com/questions/21864195
复制相似问题