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
https://forums.phpfreaks.com/topic/171101-need-help-with-inner-join/
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.

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......

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.