0
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= id1

怎么做?

4

1 回答 1

0

使用 JOIN

SELECT * 
FROM ride r inner join route_through rt on r.id = rt.id
WHERE from=$from and to=$to
于 2013-04-03T18:24:36.493 回答