我试图加入两个表中的一些列,如下所示:
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 */;
答案 0 :(得分: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;