mysql加入两个表没有共同点

时间:2013-09-30 18:15:32

标签: mysql

我有这个脚本..首先我运行查询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'];
            }
        }

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分: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中接近

编辑:抱歉,忘了加入

答案 2 :(得分: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