2013-04-03 37 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