Skip to main content

INNER JOIN not working properly



I have a problem with a mysql join i'm trying to complete. This is my code:







SELECT title, books.`author_id`, books.`publisher_id`, books.`format_id`, books.`genre_id`, pages, isbn, description, DATE_FORMAT(release_date, '%M, %d, %Y')

FROM `books`

INNER JOIN `authors` ON (`authors`.`author_id`= `books`.`author_id`)

INNER JOIN `publishers` ON (`publishers`.`publisher_id`= `books`.`publisher_id`)

INNER JOIN `formats` ON (`formats`.`format_id`= `books`.`format_id`)

INNER JOIN `genres` ON (`genres`.`genre_id`= `books`.`genre_id`)`







authors, publishers, formats and genres are all separate tables each with two columns. author_id and author in the case of the authors table, and the same format for the others.





The join doesn't seem to work no matter what i do, please help me to see where i've gone wrong.


Comments

  1. The problem is in the select part of your query.

    SELECT title, books.`author_id`, books.`publisher_id`, books.`format_id`, books.`genre_id`


    You are selecting the author_id, publisher_id, format_id and genre_id from the books table, and nothing from the joined tables, so it is no wonder that you are missing the values you want.

    SELECT title, books.`author_id`, authors.`author`, books.`publisher_id`, publishers.`publisher`, books.`format_id`, formats.`format`, books.`genre_id`, genres.`genre`


    This will produce the values you want in addition to the id values.

    ReplyDelete
  2. I think that once you've started to use escaped column names you should stick with it for whole query. The same for using prefixed column names (with table name).

    The second thing is release date, you should declare name manually for it with DATE_FORMAT(release_date, '%M, %d, %Y') AS release_date (formatting date should be matter of presentation layer not model (getting from db) so I'd be better to format date with date() when outputing it).

    Anyway your query should look like this:

    SELECT books.`title`, books.`author_id`, books.`publisher_id`, books.`format_id`,
    books.`genre_id`, books.`pages`, books.`isbn`, books.`description`,
    DATE_FORMAT(books.`release_date`, '%M, %d, %Y') AS `release_date`,
    -- Based on your commend I assume you want to use this:
    authors.`name` as `author_name` -- ...
    FROM `books`
    INNER JOIN `authors` ON (`authors`.`author_id`= `books`.`author_id`)
    INNER JOIN `publishers` ON (`publishers`.`publisher_id`= `books`.`publisher_id`)
    INNER JOIN `formats` ON (`formats`.`format_id`= `books`.`format_id`)
    INNER JOIN `genres` ON (`genres`.`genre_id`= `books`.`genre_id`)`

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.