0

我正在尝试从两个表中加入一些列,如下所示:

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

虽然我得到 12 个结果(被 4 个重复)而不是 3 个。

我在这里做错了吗?我查看了几个站点,它们似乎都使用相似的语法工作。

编辑:它似乎是将一个表中的值的数量乘以另一个 - 在这种情况下是 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 @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET NAMES utf8 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@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 CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
4

1 回答 1

0

请使用运算符“AND”而不是“&”。据我了解,您需要类似的东西(不知道您是否需要 where clouse):

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;
于 2013-07-14T18:38:14.050 回答