搜索通过许多城市的路线

时间:2013-04-03 18:21:56

标签: php mysql sql

CREATE TABLE IF NOT EXISTS `ride` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `from` text CHARACTER SET utf8 COLLATE utf8_polish_ci NOT NULL,
  `to` text CHARACTER SET utf8 COLLATE utf8_polish_ci NOT NULL,
  PRIMARY KEY (`id`)
) 

CREATE TABLE IF NOT EXISTS `route_through` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `id_ride` int(11) NOT NULL,
  `city` text COLLATE utf8_polish_ci NOT NULL,
  PRIMARY KEY (`id`)
) 

我需要搜索ride

SELECT * FROM ride WHERE from=$from and to=$to

如何搜索route_through中是否有城市?

例如:

ride表格:

id = 1

from =巴黎

to =伦敦

route_through表格:

id = 1

id_ride = 1

city ='马德里'

在表单中搜索:

现在我在这个城市形成:from - 马德里to伦敦。这应该返回ride id = 1

怎么做?

1 个答案:

答案 0 :(得分:0)

使用JOIN

SELECT * 
FROM ride r inner join route_through rt on r.id = rt.id
WHERE from=$from and to=$to