2013-09-30 58 views
0

我有這個腳本..首先我運行查詢1並存儲到數組然後查詢2, 使用foreach,我將它們組合並創建一個urls列表..但這需要時間。 。有沒有一種方法可以在MySQL中通過組合表來完成,即使他們沒有共同的列?mysql加入兩個表沒有共同點

查詢1

SELECT 
    c.id, 
    c.city_name, 
    r.region_name, 
    cr.country_name 
    FROM city AS c, region AS r, country AS cr 
    WHERE r.id = c.id_region 
     AND cr.id = c.id_country 
     AND cr.id IN 
      (SELECT id FROM country WHERE used = 1) 

查詢2

SELECT id, title FROM param WHERE active = 1 

foreach ($arrayCity as $city) { 
    foreach ($arrayParam as $param) { 
     $paramTitle = str_replace(' ', '+', $param['title']); 
     $url = 'http://url/city/'. $city['id'] .'/paramId/'. $param['id'] .'/'. 
       $paramTitle .'/'. $city['region_name'] .'/'. $city['city_name']; 
      } 
     } 

回答

0

如何

Select 'http://url/city/' + c.id + '/paramId/' + p.id + '/' + 
      Replace(title, ' ', '+') + '/' + r.region_Name + '/' + c.city_Name 
    From city c 
     Join region r On r.id = c.id_region 
     Join country n On n.id = c.id_country 
     cross join param p 
    Where n.Uused = 1 
     And p.active = 1  
1

你不必加入一個條件。不這樣做被稱爲CROSS JOINCROSS關鍵字是可選的。事實上,你已經這樣做了,因爲,是一個同義詞。

FROM city AS c, region AS r, country AS cr, param 
+0

雞蛋裏挑骨頭,但' '遠不是交叉連接的同義詞。它僅僅是所有聯接的pre-SQL 1992語法的一部分(表列表定界符部分)。 –

0

就是這樣。您可以從基表中選擇帶有結果的連接字符串。

SELECT 
    c.id, 
    c.city_name, 
    r.region_name, 
    cr.country_name, 
    ('http://url/city/' + c.city_name + '/' + p.param + '/' + c.id) AS URL 
    FROM city AS c, region AS r, country AS cr 
    JOIN param p ON c.id = p.id 
    WHERE r.id = c.id_region AND cr.id = c.id_country AND cr.id IN (SELECT id FROM country WHERE used = 1) 

我的經驗是在TSQL,但它會在我的SQL接近

編輯:對不起,忘了加入