2013-03-07 76 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 

表結構,請訪問以下鏈接: