我一直在尝试从多个tag_ids到pub_ids的单个表上获得此查询修改的预期结果
SELECT t1.pub_id
FROM `wp_teachpress_relation` AS t1
LEFT JOIN `wp_teachpress_relation` AS t2 ON t2.pub_id =356
AND t1.pub_id =1
WHERE t1.pub_id != t2.pub_id
356是“美国”的标签ID 和1是“university”的标签ID
我希望所有pub_id都有tag_id(1)大学,但没有tag_id(356)美国...我想我错过的是这个查询是在同一个桌面上运行的吗?
数据和反映结果的示例:
-- phpMyAdmin SQL Dump
-- version 2.11.11.3
-- http://www.phpmyadmin.net
--
-- Host: 68.178.140.228
-- Generation Time: Sep 02, 2013 at 11:08 AM
-- Server version: 5.0.96
-- PHP Version: 5.1.6
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Database: `eco1308903180169`
--
-- --------------------------------------------------------
--
-- Table structure for table `wp_teachpress_relation`
--
CREATE TABLE `wp_teachpress_relation` (
`con_id` int(10) unsigned NOT NULL auto_increment,
`pub_id` int(10) unsigned default NULL,
`tag_id` int(10) unsigned default NULL,
PRIMARY KEY (`con_id`),
KEY `pub_id` (`pub_id`),
KEY `tag_id` (`tag_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=25618 ;
--
-- Dumping data for table `wp_teachpress_relation`
--
INSERT INTO `wp_teachpress_relation` VALUES(10, 10285, 1);
INSERT INTO `wp_teachpress_relation` VALUES(20, 10285, 306);
INSERT INTO `wp_teachpress_relation` VALUES(30, 10286, 1);
INSERT INTO `wp_teachpress_relation` VALUES(40, 10286, 306);
INSERT INTO `wp_teachpress_relation` VALUES(50, 10287, 1);
INSERT INTO `wp_teachpress_relation` VALUES(60, 10287, 356);
INSERT INTO `wp_teachpress_relation` VALUES(62, 10287, 299);
INSERT INTO `wp_teachpress_relation` VALUES(70, 10288, 1);
INSERT INTO `wp_teachpress_relation` VALUES(80, 10288, 356);
INSERT INTO `wp_teachpress_relation` VALUES(90, 10288, 299);
好的,所以在小提琴上运行查询给了我正确的结果......也许问题来自完整查询中的其他联接?
SELECT r.tag_id tag_id, t.name name, count( r.tag_id ) tagPeak
FROM (
SELECT t1.pub_id
FROM wp_teachpress_relation AS t1
LEFT JOIN wp_teachpress_relation AS t2 ON t1.pub_id = t2.pub_id
AND t2.tag_id =356
WHERE t1.tag_id =1
AND t2.pub_id IS NULL
)pubs
JOIN `wp_teachpress_relation` r ON pubs.pub_id = r.pub_id
JOIN `wp_teachpress_tags` t ON t.tag_id = r.tag_id
WHERE r.tag_id <>1
AND t.name LIKE 'v%'
GROUP BY tag_id
ORDER BY name ASC
LIMIT 0 , 30
附加表格出版物
-- phpMyAdmin SQL Dump
-- version 2.11.11.3
-- http://www.phpmyadmin.net
--
-- Host: 68.178.140.228
-- Generation Time: Sep 02, 2013 at 12:51 PM
-- Server version: 5.0.96
-- PHP Version: 5.1.6
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Database: `eco1308903180169`
--
-- --------------------------------------------------------
--
-- Table structure for table `wp_teachpress_pub`
--
CREATE TABLE `wp_teachpress_pub` (
`pub_id` int(10) unsigned NOT NULL auto_increment,
`title` varchar(500) default NULL,
`type` varchar(50) default NULL,
`bibtex` varchar(50) default NULL,
`author` varchar(500) default NULL,
`editor` varchar(500) default NULL,
`isbn` varchar(50) default NULL,
`url` text,
`date` date default NULL,
`urldate` date default NULL,
`booktitle` varchar(200) default NULL,
`issuetitle` varchar(200) default NULL,
`journal` varchar(200) default NULL,
`volume` varchar(40) default NULL,
`number` varchar(40) default NULL,
`pages` varchar(40) default NULL,
`publisher` varchar(500) default NULL,
`address` varchar(300) default NULL,
`edition` varchar(100) default NULL,
`chapter` varchar(40) default NULL,
`institution` varchar(200) default NULL,
`organization` varchar(200) default NULL,
`school` varchar(200) default NULL,
`series` varchar(200) default NULL,
`crossref` varchar(100) default NULL,
`abstract` text,
`howpublished` varchar(200) default NULL,
`key` varchar(100) default NULL,
`techtype` varchar(200) default NULL,
`comment` text,
`note` text,
`image_url` varchar(400) default NULL,
`rel_page` int(11) default NULL,
`is_isbn` int(1) default NULL,
PRIMARY KEY (`pub_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=11764 ;
--
-- Dumping data for table `wp_teachpress_pub`
--
INSERT INTO `wp_teachpress_pub` VALUES(10285, 'Universidad del Centro Educativo Latinoamericano Rosario', 'misc', 'sc-2014-31', '', '', '', 'http://www.ucel.edu.ar/', '2012-06-22', '0000-00-00', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 0, 1);
INSERT INTO `wp_teachpress_pub` VALUES(10286, 'Universidad del Cine - Buenos Aires', 'misc', 'sc-2014-32', '', '', '', 'http://www.ucine.edu.ar/', '2012-06-22', '0000-00-00', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 0, 1);
INSERT INTO `wp_teachpress_pub` VALUES(10287, 'Instituto Universitario Aeronáutico', 'misc', 'sc-2014-33', '', '', '', 'http://www.iua.edu.ar/', '2012-06-22', '0000-00-00', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 0, 1);
INSERT INTO `wp_teachpress_pub` VALUES(10288, 'Universidad del Museo Social Argentino', 'misc', 'sc-2014-34', '', '', '', 'http://www.umsa.edu.ar/', '2012-06-22', '0000-00-00', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 0, 1);
附加标签表
-- phpMyAdmin SQL Dump
-- version 2.11.11.3
-- http://www.phpmyadmin.net
--
-- Host: 68.178.140.228
-- Generation Time: Sep 02, 2013 at 01:02 PM
-- Server version: 5.0.96
-- PHP Version: 5.1.6
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Database: `eco1308903180169`
--
-- --------------------------------------------------------
--
-- Table structure for table `wp_teachpress_tags`
--
CREATE TABLE `wp_teachpress_tags` (
`tag_id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(300) default NULL,
PRIMARY KEY (`tag_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=357 ;
--
-- Dumping data for table `wp_teachpress_tags`
--
INSERT INTO `wp_teachpress_tags` VALUES(306, 'Chile');
INSERT INTO `wp_teachpress_tags` VALUES(64, 'China');
INSERT INTO `wp_teachpress_tags` VALUES(365, 'United states');
INSERT INTO `wp_teachpress_tags` VALUES(1, 'university');
INSERT INTO `wp_teachpress_tags` VALUES(299, 'california');
答案 0 :(得分:0)
SELECT t1.pub_id
FROM wp_teachpress_relation AS t1
LEFT JOIN wp_teachpress_relation AS t2 ON t1.pub_id = t2.pub_id
WHERE t1.pub_id = 1
group by t1.pub_id
HAVING count(t2.pub_id = 356) = 0
答案 1 :(得分:0)
这是一个标准的LEFT JOIN习语,你应该学习它:
SELECT t1.pub_id
FROM wp_teachpress_relation AS t1
LEFT JOIN wp_teachpress_relation AS t2 ON t1.pub_id = t2.pub_id and t2.tag_id = 356
WHERE t1.tag_id = 1
and t2.pub_id IS NULL