1

I'm reconstructing a search query coz it's becoming redundant in "What I see" and I'm wondering what

(albums_artists, artists) ( ) does in join? is it for boosting performance?

A query that uses simple inner joins, using the old (SQL-89) implicit join syntax:

    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

Accepting also for opinions for enhancements and errors that I need to anticipate.

3
  • I see the COUNT but I don't see the GROUP BY... is that giving you the expected results? :/ Oct 7, 2013 at 6:54
  • @MostyMostacho, sorry I forgot to add it. Please see updates now :) Oct 7, 2013 at 7:10
  • 1
    This bit looks very odd: "AND albums.id = albums_artists.artist_id"
    – Strawberry
    Oct 7, 2013 at 8:10

2 Answers 2

1

As far as I know those parentheses are redundant and don't aid the query execution - it may have been thought that by surrounding them those tables would be treated differently but I'm not aware of this being true (or even needed here).

By the way, is it vma_users or ma_users? (most references are to ma_users without a v)

Personally I don't favour the way MySQL allows the GROUP BY to be 'lazy' and prefer to list out all the fields involved in the grouping. And, I wouldn't stick with those old join styles either. Don't see any syntax errors, and for enhancements I don't think there's enough information to comment.

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

well: I missed the inconsistency; I agree with those who spotted it INNER JOIN albums_artists ON albums.id = albums_artists.artist_id does not look right

0

Basically, your query should look like this, but why would the id of the album correspond to the id of the artist? That bit must be wrong. You must mean album_id, so I've changed it:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.