1

我是 MySQL 的初学者,并试图在 MySQL 中创建连接查询。

我的第一个 SQL 查询如下所示,它显示了 2 列投票和发布

SELECT votes, post 
FROM `wp_votes` where votes!=''
GROUP BY votes,post asc LIMIT 0 , 30

**第二张表是帖子所在的位置**

SELECT *
FROM `wp_posts`
LIMIT 0 , 30

我想做的是创建一个连接,以便它显示 wp_posts 表 WHERE wp_post.ID=wp_votes.post 中的所有记录,还必须检查 wp_votes.votes!=''

我尝试了以下但我坚持下去

SELECT * FROM wp_posts join wp_votes ON wp_posts.ID =wp_votes.post

下面的表结构

CREATE TABLE IF NOT EXISTS `wp_posts` (
  `ID` bigint(20) unsigned NOT NULL auto_increment,
  `post_author` bigint(20) unsigned NOT NULL default '0',
  `post_date` datetime NOT NULL default '0000-00-00 00:00:00',
  `post_date_gmt` datetime NOT NULL default '0000-00-00 00:00:00',
  `post_content` longtext NOT NULL,
  `post_title` text NOT NULL,
  `post_excerpt` text NOT NULL,
  `post_status` varchar(20) NOT NULL default 'publish',
  `comment_status` varchar(20) NOT NULL default 'open',
  `ping_status` varchar(20) NOT NULL default 'open',
  `post_password` varchar(20) NOT NULL default '',
  `post_name` varchar(200) NOT NULL default '',
  `to_ping` text NOT NULL,
  `pinged` text NOT NULL,
  `post_modified` datetime NOT NULL default '0000-00-00 00:00:00',
  `post_modified_gmt` datetime NOT NULL default '0000-00-00 00:00:00',
  `post_content_filtered` longtext NOT NULL,
  `post_parent` bigint(20) unsigned NOT NULL default '0',
  `guid` varchar(255) NOT NULL default '',
  `menu_order` int(11) NOT NULL default '0',
  `post_type` varchar(20) NOT NULL default 'post',
  `post_mime_type` varchar(100) NOT NULL default '',
  `comment_count` bigint(20) NOT NULL default '0',
  PRIMARY KEY  (`ID`),
  KEY `post_name` (`post_name`),
  KEY `type_status_date` (`post_type`,`post_status`,`post_date`,`ID`),
  KEY `post_parent` (`post_parent`),
  KEY `post_author` (`post_author`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=4570 ;

--
-- Table structure for table `wp_votes`
--

CREATE TABLE IF NOT EXISTS `wp_votes` (
  `ID` int(11) NOT NULL auto_increment,
  `post` int(11) NOT NULL,
  `votes` text NOT NULL,
  `guests` text NOT NULL,
  `usersinks` text NOT NULL,
  `guestsinks` text NOT NULL,
  PRIMARY KEY  (`ID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1052 ;
4

1 回答 1

0
SELECT  a.votes, a.post, 
        b.*
FROM    wp_votes a
        INNER JOIN wp_Post b
            ON a.post = b.ID
WHERE   a.votes <> ''

要进一步了解有关联接的更多信息,请访问以下链接:

于 2013-03-07T15:02:40.017 回答