0

我有这个脚本..首先我运行查询1并存储到数组然后查询2,使用foreach,我将它们组合并创建一个url列表..但这需要时间..有没有办法我可以做到这一点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'];
            }
        }
4

3 回答 3

1

您不必有条件加入。不这样做称为CROSS JOIN. CROSS关键字是可选的。事实上,你已经这样做了,因为,它是同义词。

FROM city AS c, region AS r, country AS cr, param
于 2013-09-30T18:18:47.193 回答
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 中会很接近

编辑:对不起,忘记加入

于 2013-09-30T18:24:45.520 回答
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       
于 2013-09-30T18:27:53.313 回答