2013-07-14 40 views
0

我試圖從兩個表中加入了一些列類似如下:MySQL的加入語句,返回的結果太多

select routes.route_collection, times.times 
from routes 
inner join times 
    on routes.bus_id & times.bus_id =1; 

雖然我得到12個結果(4複製),而不是三個。

我在這裏做錯了什麼?我查看了幾個網站,他們似乎都使用類似的語法。

編輯:它似乎是一個表中的數值乘以另一個表 - 在這種情況下,4 x 3?

這裏是數據庫模式

-- -------------------------------------------------------- 
-- Host:       127.0.0.1 
-- Server version:    5.5.32 - MySQL Community Server (GPL) 
-- Server OS:     Win32 
-- HeidiSQL Version:    8.0.0.4459 
-- -------------------------------------------------------- 

/*!40101 SET @[email protected]@CHARACTER_SET_CLIENT */; 
/*!40101 SET NAMES utf8 */; 
/*!40014 SET @[email protected]@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; 
/*!40101 SET @[email protected]@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; 

-- Dumping database structure for mydb 
CREATE DATABASE IF NOT EXISTS `mydb` /*!40100 DEFAULT CHARACTER SET utf8 */; 
USE `mydb`; 


-- Dumping structure for table mydb.buses 
CREATE TABLE IF NOT EXISTS `buses` (
    `bus_id` int(11) NOT NULL, 
    PRIMARY KEY (`bus_id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

-- Dumping data for table mydb.buses: ~5 rows (approximately) 
/*!40000 ALTER TABLE `buses` DISABLE KEYS */; 
INSERT INTO `buses` (`bus_id`) VALUES 
    (1), 
    (2), 
    (3), 
    (4), 
    (5); 
/*!40000 ALTER TABLE `buses` ENABLE KEYS */; 


-- Dumping structure for table mydb.routes 
CREATE TABLE IF NOT EXISTS `routes` (
    `route_id` int(11) NOT NULL, 
    `bus_id` int(11) DEFAULT NULL, 
    `route_collection` varchar(45) DEFAULT NULL, 
    `stop_order` int(11) DEFAULT NULL, 
    PRIMARY KEY (`route_id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

-- Dumping data for table mydb.routes: ~4 rows (approximately) 
/*!40000 ALTER TABLE `routes` DISABLE KEYS */; 
INSERT INTO `routes` (`route_id`, `bus_id`, `route_collection`, `stop_order`) VALUES 
    (1, 1, 'Douglas,Onchan,Ballasalla,Castletown,Colby', 1), 
    (2, 1, 'Douglas,Onchan,Ballasalla,Castletown,Colby', 2), 
    (3, 1, 'Douglas,Onchan,Ballasalla,Castletown,Colby', 3), 
    (4, 1, 'Douglas,Onchan,Ballasalla,Castletown,Colby', 4); 
/*!40000 ALTER TABLE `routes` ENABLE KEYS */; 


-- Dumping structure for table mydb.times 
CREATE TABLE IF NOT EXISTS `times` (
    `time_id` int(11) NOT NULL, 
    `start_time` int(11) DEFAULT NULL, 
    `bus_id` int(11) DEFAULT NULL, 
    `times` varchar(500) DEFAULT NULL, 
    `period` varchar(45) DEFAULT NULL, 
    PRIMARY KEY (`time_id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

-- Dumping data for table mydb.times: ~3 rows (approximately) 
/*!40000 ALTER TABLE `times` DISABLE KEYS */; 
INSERT INTO `times` (`time_id`, `start_time`, `bus_id`, `times`, `period`) VALUES 
    (1, 600, 1, '06:00,07:00,12:00,14:00,23:00', 'MonFri'), 
    (2, 615, 1, '06:15,07:15,12:15,14:15,23:15', 'MonFri'), 
    (3, 600, 1, '06:00,07:00,12:00,14:00,23:00', 'Sat'); 
/*!40000 ALTER TABLE `times` ENABLE KEYS */; 
/*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */; 
/*!40014 SET FOREIGN_KEY_CHECKS=IF(@OLD_FOREIGN_KEY_CHECKS IS NULL, 1, @OLD_FOREIGN_KEY_CHECKS) */; 
/*!40101 SET [email protected]_CHARACTER_SET_CLIENT */; 
+0

我很好奇哪裏的網站是使用這種語法? – Nanne

+0

我不明白你爲什麼認爲會有三個結果。 – Strawberry

回答

0

請使用操作符 'AND' 而不是 '&'。 而且據我所知,你需要類似的東西(不知道你需要的地方克勞斯):

select routes.route_collection, times.times 
from routes 
inner join times 
    on routes.bus_id = times.bus_id 
where routes.bus_id = 1; 

可能是你需要的時間累計值? 這樣的事情:

select routes.route_collection, GROUP_CONCAT(times.times) 
from routes 
inner join times 
    on routes.bus_id = times.bus_id 
where routes.bus_id = 1 
group by routes.route_id; 
+0

這返回完全相同的結果我得到(12結果,而不是3) – Mike

+0

你可以發佈(例如http://pastie.org/)SQL轉儲來測試它嗎? – user1759572

+0

請參閱我的文章 – Mike