Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何组合来自不同表的两个查询的结果?_Php_Mysql_Codeigniter - Fatal编程技术网

Php 如何组合来自不同表的两个查询的结果?

Php 如何组合来自不同表的两个查询的结果?,php,mysql,codeigniter,Php,Mysql,Codeigniter,大家好,我有两张桌子 1.桌上小铺 2.2表4-2城市 1.表4车间:- Fields : id, shopname, address, area_name, contact 2.表4城市:- Fields : id, area_name(same as table_shop), city 两个表中的公共字段:区域名称 现在我想用地区名称和城市搜索商店。 第一个优先级是区域名称,如果可用数据较少,则按城市提取 例如。 我想获取特定区域(如“river front”)移动商店的3条记录,但这里

大家好,我有两张桌子 1.桌上小铺 2.2表4-2城市

1.表4车间:-

Fields : id, shopname, address, area_name, contact
2.表4城市:-

Fields : id, area_name(same as table_shop), city
两个表中的公共字段:区域名称

现在我想用地区名称和城市搜索商店。 第一个优先级是区域名称,如果可用数据较少,则按城市提取

例如。 我想获取特定区域(如“river front”)移动商店的3条记录,但这里只有1条记录可用,但我需要3条,因此剩余的2条记录来自river front的当地城市艾哈迈达巴德。所以我需要从艾哈迈达巴德车站路去拿下两张唱片

我试着做两个不同的查询,如下所示

我的代码:-

 $q1=mysql_fetch_array(mysql_query("select * from Table_Shop where area_name='river front'"));

$tot_q1=count($q1);

if($tot_q1<3)

{

  $q2=mysql_fetch_array(mysql_query("select * from Table_City where city='ahmedabad' and area_name!='river front'"));

// i add this line *area_name!='river front'* to stop duplicate value such as if river front(mobile shop) is already fetched then i dont need to fetch it again.

}

return $q1+q2;
$q1=mysql\u fetch\u数组(mysql\u查询(“从表中选择*,区域名称为river front”);
$tot_q1=计数($q1);
如果($tot_q1尝试以下方法

SELECT * from (
    SELECT table_shop.id, table_shop.shopname, table_shop.address, 
table_shop.area_name, table_city.city
        FROM table_shop
        JOIN table_city on table_shop.area_name = table_city.area_name
        WHERE area_name='river front'
    UNION
    SELECT table_shop.id, table_shop.shopname, table_shop.address, table_shop.area_name, table_city.city
        FROM table_shop
        JOIN table_city on table_shop.area_name = table_city.area_name
        WHERE area_name!='river front' 
            AND table_city.city='ahmedabad') limit 3 

table_shop
更改为

shop {id, name, address, area_id, contact}
city {id, name}
并将城市表更改为

shop {id, name, address, area_id, contact}
city {id, name}

另外,在表前面加上
table\uu
是多余的,所以我会避免这种情况。

为了清楚起见,您正在尝试从两个不同的查询中获取数据?使用join而不是所有多个查询try
return array\u merge($q1+q2;)
是的,但我希望使用单个查询获取此数据,因为如果我同时使用两个查询,则在分页时会出现问题,因此我希望仅使用单个查询获取数据。是否可能?
city {id, name}