2013-04-03 38 views
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伦敦。这应该返回rideid = 1

如何做到这一点?

回答

0

使用JOIN

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

请记住,一个'ride'可以有多个'route_through'记录,在这种情况下该查询将返回包含** **一样乘坐多行,但每一个**不同** route_through – thaJeztah 2013-04-03 18:54:46

+0

@thaJeztah然后他可以添加额外的逻辑。 – 2013-04-03 18:55:44

+0

同意,只是把我的意见通知OP的后果,因为他显然不是很有经验。如果只需要'route_through'的*名称*,一个简单的'GROUP_CONCAT()'就足够了 – thaJeztah 2013-04-03 18:59:12