簡體   English   中英

如何合並來自不同表的兩個查詢的結果?

[英]How to combine results of two query from different tables?

我有兩張桌子:

  1. 表店:

    字段:id, shopname, address, area_name, contact

  2. 表_城市:

    字段:id、area_name(同table_shop)、city

兩個表中的公共字段:area_name

現在我想使用 area_name 和城市搜索商店。 如果可用數據較少,則優先級為 area_name,然后按城市獲取。

例如看到這個

我想獲取特定區域(例如“河濱”)的移動商店的 3 條記錄,但這里只有 1 條記錄可用,但我需要 3 條,所以剩下的 2 條記錄來自艾哈邁達巴德當地的河濱城市。 所以我需要從艾哈邁達巴德的車站路獲取下兩條記錄。

我試着做這兩個不同的查詢,如下所示,

 $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;

但是當我在這段代碼中實現分頁時它更復雜。 我怎么能在一個查詢中做到這一點?

嘗試類似的東西

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 

或者

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 table_shop.area_name='river front' OR  table_city.city='ahmedabad'
    ORDER BY table_shop.area_name <> 'river front',
    priority 
    limit 3

此外,通過文本列area_name鏈接表也不是一個好主意。 我建議你創建第三個表,

area {id, name, city_id}

table_shop更改為

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

並將table_city更改為

city {id, name}

此外,在表前加上table_是多余的,所以我會避免它。

MariaDB [sandbox]> drop table if exists table_shop;
Query OK, 0 rows affected (0.10 sec)

MariaDB [sandbox]> create table table_shop (id int,shopname varchar(20),areaname varchar(20));
Query OK, 0 rows affected (0.18 sec)

MariaDB [sandbox]>
MariaDB [sandbox]> drop table if exists table_city;
Query OK, 0 rows affected (0.08 sec)

MariaDB [sandbox]> create table table_city (id int, areaname varchar(20), city varchar(20));
Query OK, 0 rows affected (0.19 sec)

MariaDB [sandbox]>
MariaDB [sandbox]> insert into table_shop values
    -> (1,'Mob','station road'),(2,'food','river road'),(3,'cold drink','river road'),(4,'mob','river front'),
    -> (5,'rec','station road'),(6,'Mob','station road'),(7,'mob','station road'),(8,'ice','river road'),
    -> (9,'mob','river road');
Query OK, 9 rows affected (0.03 sec)
Records: 9  Duplicates: 0  Warnings: 0

MariaDB [sandbox]>
MariaDB [sandbox]> insert into table_city values
    -> (1,'station road','ahamabad'),(2,'river road','delhi'),(3,'river front','ahamabad');
Query OK, 3 rows affected (0.05 sec)
Records: 3  Duplicates: 0  Warnings: 0

MariaDB [sandbox]>
MariaDB [sandbox]>
MariaDB [sandbox]> select ts.id,ts.shopname, ts.areaname, tc.city, s.*
    -> from table_shop ts
    -> join table_city tc on tc.areaname = ts.areaname
    -> join
    -> (
    -> select  distinct ts.shopname, ts.areaname, tc.city
    -> from table_shop ts
    -> join table_city tc on tc.areaname = ts.areaname
    -> where ts.areaname = 'river front'
    -> ) s on s.shopname = ts.shopname and s.city = tc.city
    ->
    -> ;
+------+----------+--------------+----------+----------+-------------+----------+
| id   | shopname | areaname     | city     | shopname | areaname    | city     |
+------+----------+--------------+----------+----------+-------------+----------+
|    1 | Mob      | station road | ahamabad | mob      | river front | ahamabad |
|    4 | mob      | river front  | ahamabad | mob      | river front | ahamabad |
|    6 | Mob      | station road | ahamabad | mob      | river front | ahamabad |
|    7 | mob      | station road | ahamabad | mob      | river front | ahamabad |
+------+----------+--------------+----------+----------+-------------+----------+
4 rows in set (0.00 sec)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM