查询 - 在简单的mysql内连接中定义(表,表)

时间:2013-10-07 06:51:41

标签: mysql sql join

我正在重建一个搜索查询,因为它在“我看到的内容”中变得多余,我想知道

  

(专辑_艺术家,艺术家)   ()会加入吗?它是为了提升表现吗?

使用简单内连接的查询,使用旧的(SQL-89)隐式连接语法:

    SELECT 
    ma_users.name, 
    ma_users.username, 
    albums.id AS album_id, 
    albums.upc, 
    albums.name AS album_name,
    albums.status, 
    albuminfos.label, 
    DATE_FORMAT(albums.created, '%Y-%m-%d') AS created_date,
    CONCAT(artists.name) AS artist_name,
    COUNT(tracks.id) AS total_tracks,
    albumstatus.description AS album_status
    FROM albums, albuminfos, ma_users , (albums_artists, artists) , tracks ,(albumstatus, albumtypes)
    WHERE 
    albums.id = albuminfos.id 
    AND ma_users.id = albums.account_id
    AND albums.id = albums_artists.artist_id 
    AND albums_artists.artist_id = artists.id
    AND tracks.album_id = albums.id
    AND albums.status = albumstatus.id 
    AND albumtypes.id = albums.albumtype_id
    AND albuminfos.label LIKE '%$keywords%'
    GROUP BY albums.id
    ORDER BY albuminfos.label

也接受我需要预测的增强和错误的意见。

2 个答案:

答案 0 :(得分:1)

据我所知,这些括号是多余的,不会帮助查询执行 - 可能会认为通过围绕它们会对这些表进行不同的处理,但我不知道这是真的(甚至需要这里)。

顺便问一下,是vma_users还是ma_users? (大多数参考文献是没有v的ma_users)

我个人不喜欢MySQL允许GROUP BY'懒惰'的方式,而更喜欢列出分组中涉及的所有字段。并且,我也不会坚持使用那些旧的连接样式。没有看到任何语法错误,对于增强功能,我认为没有足够的信息可以评论。

SELECT
       ma_users.NAME
     , ma_users.username
     , albums.id   AS album_id
     , albums.upc
     , albums.NAME AS album_name
     , albums.STATUS
     , albuminfos.label
     , DATE_FORMAT(albums.created, '%Y-%m-%d') AS created_date
     , CONCAT (artists.NAME) AS artist_name
     , COUNT(tracks.id) AS total_tracks
     , albumstatus.description AS album_status
FROM albums
INNER JOIN albuminfos     ON albums.id = albuminfos.id
/* Vma_users ?? */
INNER JOIN ma_users       ON albums.account_id = ma_users.id
INNER JOIN albums_artists ON albums.id = albums_artists.artist_id
INNER JOIN artists        ON albums_artists.artist_id = artists.id
INNER JOIN tracks         ON albums.id = tracks.album_id
INNER JOIN albumstatus    ON albums.STATUS = albumstatus.id
INNER JOIN albumtypes     ON albums.albumtype_id =albumtypes.id
WHERE albuminfos.label LIKE '%$keywords%'
GROUP BY
       ma_users.NAME
     , ma_users.username
     , albums.id
     , albums.upc
     , albums.NAME
     , albums.STATUS
     , albuminfos.label
     , albums.created /* assuming it's only date with times of 00:00:00 */
     , albumstatus.description
ORDER BY albuminfos.label
好吧:我错过了不一致;我同意那些发现它的人 INNER JOIN albums_artists ON albums.id = albums_artists。 artist_id 看起来不对

答案 1 :(得分:0)

基本上,您的查询应该如下所示,但为什么相册的ID与艺术家的ID相对应?那一点一定是错的。你必须指album_id,所以我改了它:

SELECT ma_users.name
     , ma_users.username
     , albums.id AS album_id
     , albums.upc
     , albums.name AS album_name
     , albums.status
     , albuminfos.label
     , DATE_FORMAT(albums.created, '%Y-%m-%d') AS created_date
     , CONCAT(artists.name) AS artist_name
     , COUNT(tracks.id) AS total_tracks
     , albumstatus.description AS album_status
  FROM albums
  JOIN albuminfos
    ON albuminfos.id = albums.id 
  JOIN ma_users
    ON ma_users.id = albums.account_id
  JOIN tracks
    ON tracks.album_id = albums.id
  JOIN albums_artists
    ON albums_artists.album_id = albums.id 
  JOIN artists
    ON artists.id = albums_artists.artist_id 
  JOIN albumstatus
    ON albumstatus.id = albums.status 
  JOIN albumtypes
    ON albumtypes.id = albums.albumtype_id
 WHERE albuminfos.label LIKE '%$keywords%'
 GROUP 
    BY albums.id
 ORDER 
    BY albuminfos.label