Search code examples
phpmysql

mysql joining two table without in common


i have this script.. first i run query 1 and store into array then query 2, using a foreach, i combine them and create a list of urls..but this takes time.. is there a way i can do this just in mysql by combining the table even do they have no common column?

query 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)

query 2

  SELECT id, title FROM  param WHERE active = 1

loop

  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'];
            }
        }

Solution

  • How about

       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