2013-03-07 75 views
1

我是MySQL的初学者,试图在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!=''

我试过以下,但我坚持它下面

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 ; 
+0

您需要发布Ť能够结构。 – Ghigo 2013-03-07 14:16:16

+0

不能,你只是添加Where条件到你现有的查询? – DevelopmentIsMyPassion 2013-03-07 14:17:45

+0

@Ghigo我添加了表结构 – user580950 2013-03-07 14:27:25

回答

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

为了进一步获得更多的知识有关加入

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

表结构,请访问以下链接: