Jump to content

Need help with inner join


HoTDaWg

Recommended Posts

hi there,

 

heres the code im using for my inner join

<?php
$query = "SELECT * FROM blog_history INNER JOIN blog_links"
			. " USING(history_shared == link_id) WHERE blog_history.history_type='3'"
			. " LIMIT $skip,$results_per_page ORDER BY blog_history.history_id DESC";
?>

 

here is the structure for these two tables:

blog_history

CREATE TABLE IF NOT EXISTS `blog_history` (
  `history_id` int(50) NOT NULL AUTO_INCREMENT,
  `history_type` varchar(250) DEFAULT NULL,
  `history_date` varchar(250) DEFAULT NULL,
  `history_shared` varchar(250) DEFAULT NULL,
  PRIMARY KEY (`history_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;

 

blog_links

CREATE TABLE IF NOT EXISTS `blog_links` (
  `link_id` int(50) NOT NULL AUTO_INCREMENT,
  `link_type` varchar(50) DEFAULT NULL,
  `link_title` varchar(500) DEFAULT NULL,
  `link_link` varchar(7000) DEFAULT NULL,
  `link_description` varchar(1500) DEFAULT NULL,
  `link_date` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`link_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

 

any ideas where i went wrong in the query structure guys?

 

any help would be greatly appreciated, thanks,

 

HoTDaWg

Link to comment
Share on other sites

Not sure what you are trying to do with the USING() clause, but your usage is incorrect. That clause is supposed to specify multiple columsn - separated by commas - which exist in both tables. Records will be matched where the values of those columns are equal.

Link to comment
Share on other sites

You can easily join the two tables like this:

 

blog_history INNER JOIN blog_links"

 

SELECT

          history.column_name1, history.column_name2,

          links.column_name1, links.column_name2

FROM

        blog_history as history, blog_links as links

WHERE

      history.id=links.id

 

 

On both tables (history & links), there is a common field to join them.

 

Hope this will helps you......

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.